Drupal 将变量传递给宏时出现的问题
我有一个显示4个链接的菜单块。如果用户具有特定角色,我想删除最后一个 菜单块在特定的细枝文件和宏中创建,如下所示:Drupal 将变量传递给宏时出现的问题,drupal,twig,drupal-8,Drupal,Twig,Drupal 8,我有一个显示4个链接的菜单块。如果用户具有特定角色,我想删除最后一个 菜单块在特定的细枝文件和宏中创建,如下所示: {% import _self as menus %} {# We call a macro which calls itself to render the full tree. @see http://twig.sensiolabs.org/doc/tags/macro.html #} {{ menus.menu_links(items, attributes, 0)
{% import _self as menus %}
{#
We call a macro which calls itself to render the full tree.
@see http://twig.sensiolabs.org/doc/tags/macro.html
#}
{{ menus.menu_links(items, attributes, 0) }}
{% set role = user.role %}
{% macro menu_links(items, attributes, menu_level, elements) %}
{% import _self as menus %}
{% if items %}
<span class='arrow-down'></span>
<ul{{ attributes.setAttribute('id', 'edit-profil-nav') }}>
{% for item in items %}
{% set item_classes = [
'col-xs-12',
'col-sm-12',
items|length == 3 ? 'col-md-4' : 'col-md-3',
item.in_active_trail ? 'active' : 'notactive',
] %}
<li{{ item.attributes.addClass(item_classes) }}>
{{ link(item.title, item.url) }}
</li>
{% endfor %}
</ul>
{{ attach_library('cnas/responsive-navigation') }}
{% endif %}
{% endmacro %}
我遇到的主要问题是我不能干预宏:我希望能够与用户变量进行比较,但是当我在宏中转储它时,用户显示null
我在寻找答案的过程中发现了很多东西,但我已经看到了所有的东西和它的反面,所以我想知道我是否能做到这一点,以及如何做到这一点
多谢各位 twig中的宏有自己的作用域。您需要将变量user作为参数传递给宏才能访问它
还可以将特殊变量_上下文传递给宏。
此变量保存传递给模板的所有变量