How to make active menu?

You want to set the class .selected active menu item and remove the link.
How to do it properly and simple (without JS)?
homepage:
 path: /
 defaults: { _controller: SiteAppBundle:Main:index }
download:
 path: /download
 defaults: { _controller: SiteAppBundle:Main:download }
entry:
 path: /entry/{name}
 defaults: { _controller: SiteAppBundle:Main:entry }

the <li><a href="{{ path('homepage') }}">HOME</a></li>
the <li><a href="{{ path('entry', {'name': 'info'}) }}">INFO</a></li>
the <li><a href="{{ path('entry', {'name': 'settings'}) }}">SETTINGS</a></li>
the <li><a href="{{ path('download') }}">DOWNLOAD</a></li>


UPD
Created variables in the main template:
{% set route = app.request.get('_route') %}
{% set entry = app.request.get('_route') == 'entry' ? app.request.get('_route_params').name : "%}

Made menu menu.html.twig and hooked it into the main template.
On a routine check for {% if %}
<li{% if route == 'homepage' %}HOME{% else %}<a href="{{ path('homepage') }}">HOME</a>{% endif %}</li>
<li{% if entry == 'info' %}INFO{% else %}<a href="{{ path('entry', {'name': 'info'}) }}">INFO</a>{% endif %}</li>
<li{% if entry == 'settings' %}SETTINGS{% else %}<a href="{{ path('entry', {'name': 'settings'}) }}">SETTINGS</a>{% endif %}</li>
<li{% if route == 'download' %}DOWNLOAD{% else %}<a href="{{ path('download') }}">DOWNLOAD</a>{% endif %}</li>

p.s. Unfortunately, using a Twig extension to use a test like this:
{% if 'homepage' is is_page %}current page "homepage"{% endif %}

not work out How to get the name of the route in Twig extension (the test)?

There will be an option even shorter, offer =D
Thank you all!
July 8th 19 at 11:14
1 answer
July 8th 19 at 11:16
Solution
1. symfony.com/doc/current/bundles/KnpMenuBundle/inde...
2. If a third-party bundles to connect not want to, you can do something like this (but brevity here does not smell):
{% if app.request.attributes.get('_route') == 'homepage' %}
 <li class="selected"><span>HOME</span></li>
{% else %}
 the <li><a href="{{ path('homepage') }}">HOME</a></li>
{% endif %}

{% if app.request.attributes.get('_route') == 'entry' and
 app.request.attributes.get('_route_params').name is defined and
 app.request.attributes.get('_route_params').name == 'info' %}
 <li class="selected"><span>INFO</span></li>
{% else %}
 the <li><a href="{{ path('entry', { 'name': 'info' }) }}">INFO</a></li>
{% endif %}

{% if app.request.attributes.get('_route') == 'entry' and
 app.request.attributes.get('_route_params').name is defined and
 app.request.attributes.get('_route_params').name == 'settings' %}
 <li class="selected"><span>SETTINGS</span></li>
{% else %}
 the <li><a href="{{ path('entry', { 'name': 'settings' }) }}">SETTINGS</a></li>
{% endif %}

{% if app.request.attributes.get('_route') == 'download' %}
 <li class="selected"><span>DOWNLOAD</span></li>
{% else %}
 the <li><a href="{{ path('download') }}">DOWNLOAD</a></li>
{% endif %}
Option 2 is preferable. Why, just the app.request.attributes.get('_route') to set page variable in the main template, and already will be shorter =). Although, I think to try through the controller to give variable pageSlug. Thank you for your answer! - Burley_Schmidt commented on July 8th 19 at 11:19
: I think it is better to make a twig extension. Would be something like
{% if app.request is match_route('path', { 'parameter': value }) %} - Candace_Bogisich commented on July 8th 19 at 11:22
BoShurik: what is the "is" in the condition? It's like non-strict equality? - Burley_Schmidt commented on July 8th 19 at 11:25
match_route in my example is not a function, and test - Candace_Bogisich commented on July 8th 19 at 11:28
: twig.sensiolabs.org/doc/advanced.html#creating-an-... - Candace_Bogisich commented on July 8th 19 at 11:31
: twig.sensiolabs.org/doc/tests/index.html - Candace_Bogisich commented on July 8th 19 at 11:34
Harder and harder =) And it all started with the menus. I'll figure it out... - Burley_Schmidt commented on July 8th 19 at 11:37
: To menu, this functionality has a few, but the tool is useful for giving concise. In the future will definitely come in handy :) - Candace_Bogisich commented on July 8th 19 at 11:40

Find more questions by tags TwigSymfony