将基模板变量覆盖到另一个bundle {Symfony 3 TWIG}的子模板


Overriding base template variables to child template from another bundle {Symfony 3 TWIG}

我已经在base.html中添加了一些变量。树枝文件我有另一个文件index.html。文件在"bundle"

我扩展了base.html。index.html中的文件。树枝这是工作很好,因为我能够看到所有的内容在基础上呈现在浏览器中,当我调用index.html。树枝,但当我试图覆盖base.html的变量。从index.html中删除文件。它不工作

这里是代码

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 %}

这应该隐藏头,但它仍然显示头,如果我做isHeader = false在base.html。文件,它工作良好

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


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

我通过在symfony配置中设置全局为twig找到了答案。yml文件这里是代码

配置。yml

twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    globals:
        isFooter: true
        isHeader: true

base.html.twig

    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8" />
            <title>{% block title %}Welcome!{% endblock %}</title>
    <link rel="icon" type="image/x-icon" href="{{ asset('favicon') }}" />
        </head>
        <body>
    {% if isHeader == true %}
        <div class="container-fluid header">
        {% include 'header.html.twig' %}
            {% block header %}
            {% endblock %}
        </div>
    {% endif %}
{% block body %}
{% endblock %}
    {% if isFooter == true %}
        <div class="footer">
        {% include 'footer.html.twig' %}
            {% block footer %}
            {% endblock %}
        </div>
    {% endif %}
    <noscript><div class="alert alert-danger">You must enable Javascript on your browser for the site to work optimally and display sections completely.</div></noscript>
        </body>
    </html>

index.html.twig

{% set isFooter         = true %}
{% set isHeader         = false %}
{% block body %}
{% endblock %}

变量isHeader = false将从基本模板中删除标题,以便它不会在调用index.html.twig时呈现

如果有其他的解决方法,请评论你的建议