add active class to link with sf2 and twig

前端 未结 10 892
故里飘歌
故里飘歌 2020-12-23 13:48

following simple code:

  • List
  • is there a simple way to add an cl

    相关标签:
    10条回答
    • 2020-12-23 14:36

      This is done with symfony 3.4, but probably something similar can be done with SF2.

      src\AppBundle\Twig\AppExtension.php

      <?php
      
      namespace AppBundle\Twig;
      
      use Symfony\Component\HttpFoundation\RequestStack;
      
      class AppExtension extends \Twig_Extension
      {
          private $requestStack;
      
          public function __construct(RequestStack $requestStack)
          {
              $this->requestStack = $requestStack;
          }
      
          public function getFunctions()
          {
              return [
                  new \Twig_SimpleFunction('activeMenu', [$this, 'activeMenu'])
              ];
          }
      
          /**
           * Pass route names. If one of route names matches current route, this function returns
           * 'active'
           * @param array $routesToCheck
           * @return string
           */
          public function activeMenu(array $routesToCheck)
          {
              $currentRoute = $this->requestStack->getCurrentRequest()->get('_route');
      
              foreach ($routesToCheck as $routeToCheck) {
                  if ($routeToCheck == $currentRoute) {
                      return 'active';
                  }
              }
      
              return '';
          }
      }
      

      Add this to services.yml

      services:
          #... some other services
          AppBundle\Twig\AppExtension:
              arguments: ["@request_stack"]
      

      Usage:

      <ul class="nav navbar-nav">
          <li class="{{ activeMenu(['form', 'edit_form']) }}"><a href="{{ path('form') }}">Form</a></li>
          <li class="{{ activeMenu(['list']) }}"><a href="{{ path('list') }}">List</a></li>
      </ul>
      
      0 讨论(0)
    • 2020-12-23 14:42

      Twig allows for conditionals and the Request object is available throughout the application. If you are including the template, to get the route you want to use:

      app.request.attributes.get('_route')
      

      If you are using the render function, you want to use:

      app.request.attributes.get('_internal')
      

      With that, you should be able to use:

      class="{% if app.request.attributes.get('_route') == '_list' %}active{% endif %}"
      

      or shorter:

      class="{{ app.request.get('_route') == '_list' ? 'active' }}"
      
      0 讨论(0)
    • 2020-12-23 14:44

      Shortest version:

      {% set route = app.request.get('_route') %}
      
       <li class="{{ route starts with 'post' ? 'open' }}"></li>
       <li class="{{ route starts with 'category' ? 'open' }}"></li>
      

      Sometimes useful:

      {% set route = app.request.get('_route') %}
      
      <li class="{{ 'post' in route ? 'open' }}"></li>
      <li class="{{ 'category' in route ? 'open' }}"></li>
      
      0 讨论(0)
    • 2020-12-23 14:50

      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.

      <a class="{% if app.request.attributes.get('_route') starts with 'book' %}active{% endif %}">Books</a>
      <a class="{% if app.request.attributes.get('_route') starts with 'author' %}active{% endif %}">Authors</a>
      

      This example works with at least Symfony 2.3.x

      0 讨论(0)
    提交回复
    热议问题