如果用户没有角色,如何在 TWIG 中加入条件?
How to put in TWIG a condition, if the user does not have the role?
在我的 Drupal 8 TWIG 文件中,我有以下代码:
{% if 'marchand_premium' in user.getroles %}
test ...
{% endif %}
如果用户具有 marchand_premium
的角色,这将显示文本
我要相反
如果用户没有 marchand_premium 角色,如何在 TWIG 中添加条件?
您可以使用 not
operator :
{% if 1 not in [1, 2, 3] %}
{# is equivalent to #}
{% if not (1 in [1, 2, 3]) %}
所以你可以这样做:
{% if 'marchand_premium' not in user.getroles %}
您可以这样使用 is_granted:
{% if is_granted('ROLE_USER') == false %}
{% endif %}
或者:
{% if not is_granted('ROLE_USER') %}
{% endif %}
在我的 Drupal 8 TWIG 文件中,我有以下代码:
{% if 'marchand_premium' in user.getroles %}
test ...
{% endif %}
如果用户具有 marchand_premium
的角色,这将显示文本我要相反
如果用户没有 marchand_premium 角色,如何在 TWIG 中添加条件?
您可以使用 not
operator :
{% if 1 not in [1, 2, 3] %}
{# is equivalent to #}
{% if not (1 in [1, 2, 3]) %}
所以你可以这样做:
{% if 'marchand_premium' not in user.getroles %}
您可以这样使用 is_granted:
{% if is_granted('ROLE_USER') == false %}
{% endif %}
或者:
{% if not is_granted('ROLE_USER') %}
{% endif %}