Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig: Include block of another template

Tags:

twig

symfony

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?

like image 302
tungsten Avatar asked Dec 24 '17 12:12

tungsten


People also ask

What is the use of embed tag in twig?

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.

How do I include a template in twig?

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"} ).

What is a twig block?

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.

How to mark an include with ignore missing in twig?

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.


1 Answers

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") }}

Symfony WebProfiler - Interesting usage of blocks and templates

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:

  1. Menu
  2. Panel
  3. Toolbar

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 -->
like image 187
albert Avatar answered Sep 30 '22 10:09

albert