Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding blocks within included Twig templates

Tags:

php

twig

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 wont let me bring in any outside html, only blocks.

I will use the 'include' tag in the example below to demonstrate the intent I'm trying to describe.

#base.html.twig {% include 'elements/header.html.twig' %} {% block content %}{% endblock %} {% include 'elements/footer.html.twig' %}  #header.html.twig <h1>This is my header</h1> {% block page_title %} Default Page Title {% endblock %}  #index.html.twig {% extends 'layouts/base.html.twig' %} {# I want to be able to do this somehow #} {% block page_title %} This is my overridden page title {% endblock %} {% block content %} here is the index page content {% endblock %} 
like image 689
bruchowski Avatar asked Jul 19 '13 21:07

bruchowski


People also ask

How do I override a twig file?

Overriding templatesLocate the template you want to override. Copy the template file from its core location into your theme folder. Rename the template according to the naming conventions in order to target a more specific subset of areas where the template is used. Modify the template according to your wish.

What is block in twig?

Blocks are used for inheritance and act as placeholders and replacements at the same time. They are documented in detail in the documentation for the extends tag. Block names must consist of alphanumeric characters, and underscores. The first char can't be a digit and dashes are not permitted.

Is twig a template engine?

Twig is a modern template engine for PHP This allows Twig to be used as a template language for applications where users may modify the template design. Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define its own custom tags and filters, and create its own DSL.

What is Twig in Symfony?

Twig is a template engine for the PHP programming language. Its syntax originates from Jinja and Django templates. It's an open source product licensed under a BSD License and maintained by Fabien Potencier. The initial version was created by Armin Ronacher.


1 Answers

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 <h1>This is my header</h1> {% 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 %} 
like image 69
Webberig Avatar answered Sep 19 '22 15:09

Webberig