php 检查 {{ path() }} 是否是 Symfony2 中的当前 {{ path() }}

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8451740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:45:18  来源:igfitidea点击:

Check if {{ path() }} is current {{ path() }} in Symfony2

phpsymfonytwig

提问by ed209

How do I check if the current page is this path:

如何检查当前页面是否是这个路径:

{{ path('someNamePath') }}

I want to set a css class to the <a>element or possible remove it altogether, e.g.

我想为<a>元素设置一个 css 类或可能将其完全删除,例如

{% if  isCurrentPath('someNamePath') %}
    <a href="{{ path('someNamePath') }}" class="YouAreHere">My Link</a>
{% else %}
    <a href="{{ path('someNamePath') }}">My Link</a>
{% endif %}

回答by Ondrej Slinták

app.request.get('_route')is probably what you are looking for:

app.request.get('_route')可能是您正在寻找的:

{% if app.request.get('_route') == 'current_route' %}
    <a href="{{ path('current_route') }}" class="YouAreHere">My Link</a>
{% else %}
    <a href="{{ path('some_other_route') }}">My Link</a>
{% endif %}

In case you want to use uri instead, you can use app.request.uri.

如果您想改用 uri,则可以使用app.request.uri.