Sunday, July 19, 2015

Append content to block from the other template

To include a template, you need to use the include keyword, not the use keyword:

{% block appendable %}

    {# Assuming your sub1 template is in AcmeDemoBundle/Resources/views/MySub/sub1.html.twig #}
    {% include "AcmeDemoBundle:MySub:sub1.html.twig" %}

{% endblock appendable %}

Using Inheritance:

If you wish, you can use the {{ parent() }} keyword to use inheritance. For example, if you want to include sub1.html.twig by default but append sub2.html.twig in your child template, you can do the following:

base.html.twig

{% block content %}

    {% include "AcmeDemoBundle:MySub:sub1.html.twig" %}

{% endblock %}

site.html.twig

{% extends base.html.twig %}

{% block content %}

    {# render what happens in the parent content block #}
    {{ parent() }}

    {# and append sub2.html.twig as well #}
    {% include "AcmeDemoBundle:MySub:sub2.html.twig" %}

{% endblock content %}

Reference:

http://stackoverflow.com/questions/14564351/append-content-to-block-from-multiple-subtemplates

No comments: