将参数传递到Twig宏

时间:2019-05-13 00:46:10

标签: php twig

我将2个参数传递给了Twig宏,但是似乎只有其中一个被传递了

{{currentPage}} //this is outputting the current page e.g. home 


{% macro render_menu(links, currentPage) %}

{% import _self as subnav %}

{% for code, link in links %}

{{currentPage}} //this is NOT outputting the current page

<li class="{{ code == currentPage ? 'active' }} {{ link.sublinks ? 'dropdown' }}">

我需要使用currentPage启用“活动”菜单项

1 个答案:

答案 0 :(得分:0)

您在哪里调用宏?该部分代码丢失。请注意,宏具有自己的范围,这意味着它们不知道任何变量,因此您必须显式地传递它们

macro.twig

{% macro render_menu(links, currentPage) %}
    {% import _self as subnav %}
    {% for link in links %}
        <li class="{{ link.code == currentPage ? 'active' }} {{ link.sublinks ? 'dropdown' }}">
            <a href="{{ link.code }}">{{ link.code }}</a>
        </li>
    {% endfor %}
{% endmacro %}

main.twig

{% import 'macro.twig' as macro %}

{{ macro.render_menu(links, currentPage) }}

demo