Navigation, highlight current page

后端 未结 7 1333
夕颜
夕颜 2020-12-14 09:15

I\'ve got a parent layout and derived from that child sites.

The parent layout has a navigation, each navigation point represents one child site.

How do i hi

7条回答
  •  再見小時候
    2020-12-14 10:19

    I know it's an old thread, but still you find it among top 3 on Google, so here's a little update to it.

    You can take different aproaches on creating a navigation with a "highlighting class" with Symfony.

    1. Check route
    As @Sebastian J suggested, you can check with an if else for the route.

    
    

    Problem: It's not officially supported, as @netmikey pointed out: How to get current route in Symfony 2?

    1.1. Check route array
    I actually use this on my projects, with one adjustment. I use an in array function, to be able to give more than one route.

    
    

    1.2. Check route start with ...
    A third approach would be what @bernland has suggested. Let's asume we want to match all routes starting with foo_products and we'd like to apply this by magic.

    
    

    As I said, I use this and haven't had an issue, yet.

    2. Use a Bundle/function I'm sure there are other Bundles out there, but I'd recomend you this to create your navigation: https://github.com/KnpLabs/KnpMenuBundle

    3. Use a macro edit July 2015
    My new favorite is to use a macro like

    {% macro menuItem(name, url, subitems) %}
        {% spaceless %}
            {% set subitems = subitems|default({}) %}
            {% set currentUrl = path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) %}
            {% set isActive = currentUrl == url %}
    
            {% for name, suburl in subitems %}
                {% set isActive = not isActive and currentUrl == suburl %}
            {% endfor %}
    
            
                 0 %} class="has-sub-menu"{% endif %}>{{ name|trans() }}
                {% if subitems|length > 0 %}
                    
      {% for name, url in subitems %} {{ _self.menuItem(name, url) }} {% endfor %}
    {% endif %}
  • {% endspaceless %} {% endmacro %}

    just add this code somewhere in your twig file (not in a {% block %} thing!)
    You then can call it like this for a single item:

    {{ _self.menuItem('FooBar', path('foo_foobar')) }}
    

    or for a item with subitems:

    {{ _self.menuItem('FooBar', path('foo_foobar'), {
        'Foo': path('foo_baz', {slug: 'foo'}),
        "Bar": path('foo_baz', {slug: 'bar'}),
    }) }}
    

    Beautiful, isn't it?

提交回复
热议问题