Twig included template extending parent's block once

后端 未结 2 1934
遇见更好的自我
遇见更好的自我 2021-02-13 11:36

Is there a way to do this? I have a template which outputs one blog article.

Now, on index page I show 10 articles by including that template in

2条回答
  •  遥遥无期
    2021-02-13 12:00

    You can use a new block (not tested):

    {# index.html.twig #}
    {% block stylesheets -%}
        {% block article_styles '' %}
    {%- endblock %}
    
    {% for ... -%}
        {% include VendorBundle:template.html.twig with {'article': article} %}
    {%- endfor %}
    

    {# template.html.twig #}
    {% block article_styles -%}
        {{ parent() }}
        
    {%- endblock %}
    
    {# ... #}
    

    Edit: Added {{ parent() }}, this will print every content the block already has.

提交回复
热议问题