首页 > 解决方案 > 从孩子传递给父母,然后包括树枝

问题描述

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

//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

解决方案


好的,我回答我自己。

//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 中设置颜色,而不是在任何块中,然后在将变量传递给包含文件时设置默认值。


推荐阅读