简体   繁体   English

将参数传递给Twig宏

[英]Passing parameters to a Twig Macro

I pass 2 parameters to a Twig macro but only one of them appears to be passed 我将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' }}">

I need to use currentPage to enable the 'active' menu item 我需要使用currentPage来启用“活动”菜单项

Where are you calling the macro? 你在哪里叫宏? That part of the code is missing. 代码的那部分缺失了。 Do note macro's have their own scope, this means they don't know any variable and you have to pass them explicitly 注意宏有自己的范围,这意味着它们不知道任何变量,你必须明确地传递它们

macro.twig 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 main.twig

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

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

demo 演示

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

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