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

时间:2016-08-21 10:27:24

标签: php twig symfony

我在 base.html.twig 文件中添加了一些变量 我在“捆绑”

中有另一个文件 index.html.twig 文件

我在 index.html.twig 中扩展了 base.html.twig 文件,该文件工作正常,因为我能够看到基础中的所有内容都在浏览器中呈现当我打电话给 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一样正常

2 个答案:

答案 0 :(得分:3)

你的方法太奇怪了,我不确定你为什么要这样做, 根据我在问题中找到的,尝试做这样的事情:

在基地:

let formatter = NSNumberFormatter()
formatter.usesGroupingSeparator = true
formatter.groupingSeparator = "."
let number = 2000000
let string = formatter.stringFromNumber(number)


在索引中:

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

答案 1 :(得分:0)

我在 symfony config.yml 文件中设置了twig的全局答案,这里是代码

config.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时不会呈现

任何其他变通办法的人请评论您的建议。

相关问题