Rupesh Terase
Rupesh Terase

Reputation: 460

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

I have added some of the variables in base.html.twig file I have another file index.html.twig file in "bundle"

I have extended base.html.twig file in index.html.twig which is working fine as I am able to see all content in base is rendered in browser when i am calling index.html.twig, but when i try to override variables of base.html.twig file from index.html.twig its not working

here is code

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

this should hide header but its still displaying header where as if I do isHeader = false in base.html.twig file it works fine

Upvotes: 6

Views: 1996

Answers (2)

Rupesh Terase
Rupesh Terase

Reputation: 460

I found answer by setting global for twig in symfony config.yml file here is code

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

variable isHeader = false will remove header from base template so that it will not render on call of index.html.twig

Any other workaround guys please comment your suggestions.

Upvotes: 0

Ali
Ali

Reputation: 493

your method is too weird , i'm not sure why are you doing this , According to what I found from question , try to do something like this :

in base :

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


in index :

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

Upvotes: 5

Related Questions