Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | ``block`` |
| 2 | ========= |
| 3 | |
| 4 | When a template uses inheritance and if you want to print a block multiple |
| 5 | times, use the ``block`` function: |
| 6 | |
| 7 | .. code-block:: html+twig |
| 8 | |
| 9 | <title>{% block title %}{% endblock %}</title> |
| 10 | |
| 11 | <h1>{{ block('title') }}</h1> |
| 12 | |
| 13 | {% block body %}{% endblock %} |
| 14 | |
| 15 | The ``block`` function can also be used to display one block from another |
| 16 | template: |
| 17 | |
| 18 | .. code-block:: twig |
| 19 | |
| 20 | {{ block("title", "common_blocks.twig") }} |
| 21 | |
| 22 | Use the ``defined`` test to check if a block exists in the context of the |
| 23 | current template: |
| 24 | |
| 25 | .. code-block:: twig |
| 26 | |
| 27 | {% if block("footer") is defined %} |
| 28 | ... |
| 29 | {% endif %} |
| 30 | |
| 31 | {% if block("footer", "common_blocks.twig") is defined %} |
| 32 | ... |
| 33 | {% endif %} |
| 34 | |
| 35 | .. seealso:: |
| 36 | |
| 37 | :doc:`extends<../tags/extends>`, :doc:`parent<../functions/parent>` |