繁体   English   中英

将参数传递给Twig宏

[英]Passing parameters to a Twig Macro

我将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来启用“活动”菜单项

你在哪里叫宏? 代码的那部分缺失了。 注意宏有自己的范围,这意味着它们不知道任何变量,你必须明确地传递它们

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) }}

演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM