Overriding blocks within included Twig templates

前端 未结 5 952
北荒
北荒 2020-12-12 21:19

Is there a generally \"good\" way to achieve this functionality? I have read about the \'use\' tag, which seems like the best option so far, but I still don\'t like that it

5条回答
  •  误落风尘
    2020-12-12 21:43

    I found a solution. Use the block() function to get the child's block content and pass it to header.html.twig as a variable in the include statement:

    #base.html.twig
    {% include 'elements/header.html.twig' with {page_title: block('page_title')} %}
    {% block content %}{% endblock %}
    {% include 'elements/footer.html.twig' %}
    
    #header.html.twig
    

    This is my header

    {% if page_title is empty %} Default Page Title {% else %} {{ page_title }} {% endif %} #index.html.twig {% extends 'layouts/base.html.twig' %} {% block page_title %} This is my overridden page title {% endblock %} {% block content %} here is the index page content {% endblock %}

提交回复
热议问题