following simple code:
<li><a href="{{ path('_list') }}">List</a></li>
is there a simple way to add an class="active"
if the current page matches the _list
route?
using the newest PR-Release of symfony2 and twig as template engine
following simple code:
<li><a href="{{ path('_list') }}">List</a></li>
is there a simple way to add an class="active"
if the current page matches the _list
route?
using the newest PR-Release of symfony2 and twig as template engine
This is done with symfony 3.4, but probably something similar can be done with SF2.
src\AppBundle\Twig\AppExtension.php
Add this to services.yml
Usage:
With ternary operator:
Sometimes you don't want to do exact matching of a route. For those cases, you can use the "starts with" conditional logic of twig.
As an example, lets assume you are working with books. You have the following routes: book, book_show, book_new, book_edit. You want the navigation item Book to be highlighted for any of those cases. This code would accomplish that.
This example works with at least Symfony 2.3.x
class="class_name {% if loop.index0 == 0 %}CLASSNAME{% endif %}"