简体   繁体   English

在 symfony 中的导航栏上从 base.html.twig 激活链接

[英]Make a link active from the base.html.twig on Navbar in symfony

I researched a lot of things on stack and any other sources.我在堆栈和任何其他来源上研究了很多东西。 I found some interesting ideas but couldn't apply them as i'm a student and cannot just yet know where to put things directly in their right spot.我发现了一些有趣的想法,但无法应用它们,因为我是一名学生,而且还不知道将东西直接放在正确的位置。

So I got this code right in the base.html.twig所以我在base.html.twig中得到了这个代码

<ul class="nav navbar-nav">
                    <li class="active">
                        <a href="{{ path('book_index') }}">Accueil<span class="sr-only">(current)</span></a>
                    </li>
                    <li><a href="{{ path('book_ourBooks') }}">Nos livres</a></li>
                    <li><a href="{{ path('book_aboutUs') }}">Être édité</a></li>
                </ul>

and I would like, without using Jquery, being able to show the active when I click on a Link and going to that page.我希望,在不使用 Jquery 的情况下,当我单击链接并转到该页面时能够显示活动状态。

I found that sort of code:我发现了这样的代码:

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

(and other things looking nearly similar) But I really don't know how and where to put it with my own link and such. (和其他看起来几乎相似的东西)但我真的不知道如何以及在哪里将它与我自己的链接等放在一起。 If any help is given, this would be really appreciated, thank you:)如果有任何帮助,将不胜感激,谢谢:)

ps: i'm using symfony 2.8 ps:我使用的是 symfony 2.8

You need to insert the code on your li's like this: 您需要像这样在li's插入代码:

           <ul class="nav navbar-nav">
                <li class="{% if app.request.attributes.get('_route') == '_RouteName' %}active{% endif %}">
                    <a href="{{ path('book_index') }}">Accueil<span class="sr-only">(current)</span></a>
                </li>
                <li class="{% if app.request.attributes.get('_route') == '_RouteName' %}active{% endif %}">
                 <a href="{{ path('book_ourBooks') }}">Nos livres</a>
                </li>
                <li class="{% if app.request.attributes.get('_route') == '_RouteName' %}active{% endif %}">
                <a href="{{ path('book_aboutUs') }}">Être édité</a>
                </li>
            </ul>

Replace _RouteName with the correct _route for each link. 为每个链接用正确的_route替换_RouteName

I know this question has been answered but I found a neat approach for a different situation which might help somebody who has submenus, just like me:我知道这个问题已经得到解答,但我找到了一种适用于不同情况的巧妙方法,这可能会帮助像我一样有子菜单的人:

You can search for a substring inside string in twig您可以在 twig 中的字符串内搜索 substring

{# returns true #}

{{ 'ca' in 'car' }}

What I did with my routes and navigation to make the links active我对路线和导航所做的以使链接处于活动状态

<li>
    <a href="{{ url('admin_agency_index') }}"
       class="{% if 'admin_agency_' in app.request.attributes.get('_route') %}active{% endif %}">
        <i class="lnr lnr-apartment"></i>
          <span>Agencies</span>
    </a>
</li>

My agency routes are named like:我的代理路线命名如下:

  • admin_agency_new admin_agency_new
  • admin_agency_edit admin_agency_edit
  • admin_agency_delete admin_agency_delete

In that case, if you reach a submenu for adding an agency for example, you will still have active class in your html.在这种情况下,例如,如果您到达添加代理的子菜单,您的 html 中仍将有活动的 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM