我目前正在開發 Symfony 4.4 Web 應用程式。
我的應用程式的管理員可以更改選單,因此有一個具有以下屬性的表:

因為我得到了帶有一些引數的路由(例如:slug 或 id,并且 slug 可以改變,所以它不是一個很好的識別符號)。
問題是我想知道我在哪個選單項上,以保持選單打開(在 < li > 項上添加活動類),但我找不到正確的方法。
我試過的:
用類似的東西識別路線:
<li>{% if app.request.get('_route') == 'foo_products_overview' and app.request.get('slug') in ["entityslug"] %} class="active" {% endif %}></li>
但是我的應用程式的每個路由的引數都不相同(使用 id 或 slug 找到一個的多個物體)。
這是我的選單項的顯示方式:
{% if child2.getMenuItems()|length > 0 %}
<ul class="collapse nav-sub" aria-expanded="false">
{% for child3 in child2.getMenuItems() if child2.getMenuItems()|length > 0 and child3.level == 4 and (is_granted(child3.roles) or child3.roles is empty) %}
<li class="{% if child3.getMenuItems()|length > 0 %}nav-dropdown{% endif %}">
<a hljs-keyword">if child3.getMenuItems()|length > 0 %}has-arrow{% endif %}"
href="{% if child3.route is not null %}{% if child3.routeParameters %}{{ path(child3.route, {'id': child3.routeParameters}) }}{% else %}{{ path(child3.route) }}{% endif %}{% else %}#{% endif %}"
aria-expanded="false">
<span>{% if "ROLE_ADMIN" in child3.roles %}<i class="la la-eye-slash text-danger mr-2"></i>{% endif %}{{ child3.name|raw }}</span>
</a>
</li>
{% endfor %}
</ul>
{% endif %}
你有沒有遇到過這個問題?
也許 KnpMenu 有辦法?( https://symfony.com/bundles/KnpMenuBundle/current/index.html ) 我暫時不使用它。
uj5u.com熱心網友回復:
你試過這個嗎:用你的變數名替換“child.id”,我認為那是“child3.id”但不確定。
class="{% if app.request.attributes.get( '_route' ) starts with 'foo_products_overview' and app.request.attributes.get('id') == child.id %}active{% endif%}"
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/368142.html
