1``include``
2===========
3
4The ``include`` statement includes a template and outputs the rendered content
5of that file:
6
7.. code-block:: twig
8
9    {% include 'header.html' %}
10        Body
11    {% include 'footer.html' %}
12
13.. note::
14
15    As of Twig 1.12, it is recommended to use the
16    :doc:`include<../functions/include>` function instead as it provides the
17    same features with a bit more flexibility:
18
19    * The ``include`` function is semantically more "correct" (including a
20      template outputs its rendered contents in the current scope; a tag should
21      not display anything);
22
23    * The ``include`` function is more "composable":
24
25      .. code-block:: twig
26
27          {# Store a rendered template in a variable #}
28          {% set content %}
29              {% include 'template.html' %}
30          {% endset %}
31          {# vs #}
32          {% set content = include('template.html') %}
33
34          {# Apply filter on a rendered template #}
35          {% apply upper %}
36              {% include 'template.html' %}
37          {% endapply %}
38          {# vs #}
39          {{ include('template.html')|upper }}
40
41    * The ``include`` function does not impose any specific order for
42      arguments thanks to :ref:`named arguments <named-arguments>`.
43
44Included templates have access to the variables of the active context.
45
46If you are using the filesystem loader, the templates are looked for in the
47paths defined by it.
48
49You can add additional variables by passing them after the ``with`` keyword:
50
51.. code-block:: twig
52
53    {# template.html will have access to the variables from the current context and the additional ones provided #}
54    {% include 'template.html' with {'foo': 'bar'} %}
55
56    {% set vars = {'foo': 'bar'} %}
57    {% include 'template.html' with vars %}
58
59You can disable access to the context by appending the ``only`` keyword:
60
61.. code-block:: twig
62
63    {# only the foo variable will be accessible #}
64    {% include 'template.html' with {'foo': 'bar'} only %}
65
66.. code-block:: twig
67
68    {# no variables will be accessible #}
69    {% include 'template.html' only %}
70
71.. tip::
72
73    When including a template created by an end user, you should consider
74    sandboxing it. More information in the :doc:`Twig for Developers<../api>`
75    chapter and in the :doc:`sandbox<../tags/sandbox>` tag documentation.
76
77The template name can be any valid Twig expression:
78
79.. code-block:: twig
80
81    {% include some_var %}
82    {% include ajax ? 'ajax.html' : 'not_ajax.html' %}
83
84And if the expression evaluates to a ``\Twig\Template`` or a
85``\Twig\TemplateWrapper`` instance, Twig will use it directly::
86
87    // {% include template %}
88
89    $template = $twig->load('some_template.twig');
90
91    $twig->display('template.twig', ['template' => $template]);
92
93You can mark an include with ``ignore missing`` in which case Twig will ignore
94the statement if the template to be included does not exist. It has to be
95placed just after the template name. Here some valid examples:
96
97.. code-block:: twig
98
99    {% include 'sidebar.html' ignore missing %}
100    {% include 'sidebar.html' ignore missing with {'foo': 'bar'} %}
101    {% include 'sidebar.html' ignore missing only %}
102
103You can also provide a list of templates that are checked for existence before
104inclusion. The first template that exists will be included:
105
106.. code-block:: twig
107
108    {% include ['page_detailed.html', 'page.html'] %}
109
110If ``ignore missing`` is given, it will fall back to rendering nothing if none
111of the templates exist, otherwise it will throw an exception.
112