I would like to include only the contents of certain block of another template. Is it possible to access only the contents of a block and not the whole file?
As far as I can see it, embed
and include
always include and output the whole file. And use
imports all blocks and apparently (?) the destination file needs to be hard-coded and cannot be an expression or a variable passed to the template. Is that correct?
Switch to the documentation for Twig 1.x . 3.x . The embed tag combines the behavior of include and extends . It allows you to include another template's contents, just like include does. But it also allows you to override any block defined inside the included template, like when extending a template.
The include () function takes two arguments: the name of the template to include and a collection of additional variables to pass. These variables are a key-value list of names and their values. In Twig, a key-value array is called a "hash", and uses a syntax that's just like JavaScript or JSON (i.e. {"foo": "bar"} ).
The Twig block is what you see identified by the {% block %} and {% endblock %} tags. The word "content" is the identifier for the block. You can have multiple blocks in a single template.
You can mark an include with ignore missing in which case Twig will ignore the statement if the template to be included does not exist. It has to be placed just after the template name. Here some valid examples: You can also provide a list of templates that are checked for existence before inclusion.
Use a macro https://twig.symfony.com/doc/2.x/tags/macro.html
Render a block template (used by web profiler): https://twig.symfony.com/doc/2.x/functions/block.html
{{ block("title", "common_blocks.twig") }}
The Symfony WebProfiler it is a great example:
vendor/symfony/symfony/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/request.html.twig
Each profiler view template has 3 blocks:
Then it renders each block depending on when it is required.
Toolbar example: vendor/symfony/symfony/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar.html.twig
<!-- START of Symfony Web Debug Toolbar -->
<div id="sfMiniToolbar-{{ token }}" class="sf-minitoolbar" data-no-turbolink>
<a href="#" title="Show Symfony toolbar" tabindex="-1" id="sfToolbarMiniToggler-{{ token }}" accesskey="D">
{{ include('@WebProfiler/Icon/symfony.svg') }}
</a>
</div>
<div id="sfToolbarClearer-{{ token }}" class="sf-toolbar-clearer"></div>
<div id="sfToolbarMainContent-{{ token }}" class="sf-toolbarreset clear-fix" data-no-turbolink>
{% for name, template in templates %}
{% if block('toolbar', template) is defined %}
{% with {
collector: profile.getcollector(name),
profiler_url: profiler_url,
token: profile.token,
name: name,
profiler_markup_version: profiler_markup_version,
csp_script_nonce: csp_script_nonce,
csp_style_nonce: csp_style_nonce
} %}
{{ block('toolbar', template) }}
{% endwith %}
{% endif %}
{% endfor %}
<a class="hide-button" id="sfToolbarHideButton-{{ token }}" title="Close Toolbar" tabindex="-1" accesskey="D">
{{ include('@WebProfiler/Icon/close.svg') }}
</a>
</div>
<!-- END of Symfony Web Debug Toolbar -->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With