php - 将基本模板变量覆盖到另一个包 {Symfony 3 TWIG} 的子模板

标签 php twig symfony

我在 base.html.twig 文件中添加了一些变量 我在“ bundle ”中有另一个文件 index.html.twig 文件

我在 index.html.twig 中扩展了 base.html.twig 文件,它工作正常,因为我能够看到 base 中的所有内容都在浏览器中呈现当我调用 index.html.twig 时,但是当我尝试从 index.html.twig 覆盖 base.html.twig 文件的变量时> 它不工作

这是代码

base.html.twig

    <!DOCTYPE html>
        <html>
            <head>
                <meta charset="UTF-8" />
                <title>{% block title %}Welcome!{% endblock %}</title>
                {% block stylesheets %}

                {% endblock %}
                <link rel="icon" type="image/x-icon" href="{{ asset('favicon') }}" />
            </head>
            <body>
           {% set isHeader      = (isHeader|default(true)) %}

{% if isHeader == true %}
            <div class="container-fluid header">
                {% include 'header.html.twig' %}
                {% block header %}

                {% endblock %}
            </div>
            {% endif %}

    </body>
    </html>

index.html.twig

{% extends 'base.html.twig' %}

{% set isHeader         = false %}

这应该隐藏标题,但它仍然显示标题,就像我在 base.html.twig 文件中执行 isHeader = false 一样工作正常

最佳答案

你的方法太奇怪了,我不知道你为什么要这样做, 根据我从问题中发现的,尝试做这样的事情:

在基地:

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8" />
            <title>{% block title %}Welcome!{% endblock %}</title>
            {% block stylesheets %}

            {% endblock %}
            <link rel="icon" type="image/x-icon" href="{{ asset('favicon') }}" />
        </head>
        <body>
       {%block top_header %}
            <div class="container-fluid header">
               {% include 'header.html.twig' %}
                {% block header %}

                {% endblock %}
            </div>
        {%endblock%}
</body>
</html>


在索引中:

{% extends 'base.html.twig' %}
{% block top_header %}{% endblock %} //keep this empty , remove the top_header content

关于php - 将基本模板变量覆盖到另一个包 {Symfony 3 TWIG} 的子模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39063189/

相关文章:

php - 编辑 mysql 数据库中的数据

mongodb - 当 child 是拥有方时,Doctrine Mongo 级联删除 child

authentication - 在安全或自定义提供程序之前加载监听器

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows 等...期望参数 1 是资源

php - Jenkins pdepend插件错误

php 为什么或运算符返回 0 为真

symfony - 获取并使用表单定义 UserType.php 中的转换器

php - Twig "Unable to add function"

Symfony 4 Twig 模板错误 : Variable "widget" does not exist

php - 使用 Silex 框架在 Twig 模板中使用 Javascript 变量