【问题标题】:Pass set from child to parent then to include twig从孩子传递给父母,然后包括树枝
【发布时间】:2021-11-13 02:01:56
【问题描述】:

您好,我需要从子文件传递颜色变量以包含在父文件中。 我试着做一个简单的例子:

//File: _navbar.html.twig
<header class="navbar navbar-{{ color }}">...</header>

//File: base.html.twig
<body>
{{ include('_navbar.html.twig', {color: color}) }}

{% block content %}{% endblock %}

{{ include('_footer.html.twig') }}
</body>

//File: theme.html.twig
I want to pass color from here

我尝试在 base.html.twig 文件中创建一个块,如下所示:

{% block navbar_style %}{% set color="light" %}{% endblock %}

并重写theme.html.twig中的块,但它不起作用。

有什么想法吗?

【问题讨论】:

    标签: twig


    【解决方案1】:

    好的,我自己回答。

    //File: _navbar.html.twig
    <header class="navbar navbar-{{ color }}">...</header>
    
    //File: base.html.twig
    <body>
    {{ include('_navbar.html.twig', {color: color ?? 'light'}) }}
    
    {% block content %}{% endblock %}
    
    {{ include('_footer.html.twig') }}
    </body>
    
    //File: theme.html.twig
    {% set color="dark" %}
    

    我直接在 theme.html.twig 中设置颜色,而不是在任何块中,然后我在将变量传递给包含文件时设置默认值。

    【讨论】:

      猜你喜欢
      • 2020-07-22
      • 1970-01-01
      • 2018-06-29
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      相关资源
      最近更新 更多