问题确实是“简单的”但同时又很困难。
所以我想要这个
{% if currentUrl != '/' %}
同时匹配
'/home/index'
我在这里尝试过,但没有一个起作用。
{% if currentUrl != '/' or currentUrl == '/home/index'%}
{% if currentUrl != '/' or currentUrl != '/home/index'%}
{% if (currentUrl != '/') or (currentUrl != '/home/index') %}
还有更多..
这是我的语法,还是为什么我得到的结果与
不同? {% if currentUrl != '/' %}
谢谢
答案 0 :(得分:3)
您错误地使用了逻辑或比较。
{% if currentUrl != '/' or currentUrl != '/home/index'%}
{# it is not one of them, but it could still be the other one #}
{# it's never both of them so this block will always render #}
{% endif %}
我认为您的意图是执行逻辑与比较。
{% if currentUrl != '/' and currentUrl != '/home/index'%}
{# it is not one AND it is also not the other #}
{# this block will not render if it is either of the specified urls #}
{% endif %}
但是,在您的情况下,我建议使用树枝in
Containment Operator。如果左侧的操作数包含在右侧,则执行包含测试,返回true。这样可以使代码更具可读性,并且如果以后决定需要匹配另一个URL,只需将其添加到数组中,就可以更轻松地进行维护。
{% if currentUrl in ['/', '/home/index'] %}
{# it's one of the urls #}
{% else %}
{# it's a different url #}
{% endif %}
如果左边的操作数不包含在右边,则使用not in
返回true。
{% if currentUrl not in ['/', '/home/index'] %}
{# it's a different url #}
{% else %}
{# it's one of the urls #}
{% endif %}