twig - 从孩子传递给父母,然后包括树枝
问题描述
您好我需要从子文件中传递颜色变量以包含在父文件中。我试着做一个简单的例子:
//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 中的块,但它不起作用。
有任何想法吗?
解决方案
好的,我回答我自己。
//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 中设置颜色,而不是在任何块中,然后在将变量传递给包含文件时设置默认值。
推荐阅读
- javascript - 按钮将数据保存在 .txt 文件中并擦除其中的旧数据
- angular - formGroup.getRawValue() 以角度引发错误
- javascript - CryptoJS.enc.Hex.parse(hash) 做什么,以及如何在 Java 中复制它?
- javascript - 不允许在 jQuery 中选择超过 'n'
- ios - 围绕中心快速旋转动画
- java - 如何在 jlist 中显示指定期间的每个月?
- r - 在R中设置空间点数据框
- ocaml - Ocaml 语法很奇怪
- python - 将较少的参数传递给函数然后在 Python 中定义
- python - 在回答 Webrequest 时做出反应 - Python 3.7