Apply "currentClass" in link in item
guillaumecardon opened this issue · comments
Hello,
Using this Twig syntax : {{ knp_menu_render('main', {'currentClass' : 'active'}) }}
, the css class "active" is applied to "li" tag.
However, is there an option or specific syntax to apply the currentClass on link inside the "li" tag ?
Actual result :
<li class="nav-item active"><a href="/user/" class="nav-link">Users</a></li>
Expected result :
<li class="nav-item active"><a href="/user/" class="nav-link active">Users</a></li>
or
<li class="nav-item"><a href="/user/" class="nav-link active">Users</a></li>
Thanks
You can use a custom template for that.
Ok, after many tests to understand the logic, this is a solution :
{% block linkElement %}
{% import _self as knp_menu %}
{%- set classes = item.linkAttribute('class') is not empty ? [item.linkAttribute('class')] : [] %}
{%- if matcher.isCurrent(item) %}
{%- set classes = classes|merge([options.currentClass]) %}
{%- endif %}
{%- set attributes = item.linkAttributes %}
{%- if classes is not empty %}
{%- set attributes = attributes|merge({'class': classes|join(' ')}) %}
{%- endif %}
<a href="{{ item.uri }}"{{ knp_menu.attributes(attributes) }}>
{{ block('label') }}
</a>
{% endblock %}