# `include`
The `include` statement includes a template and returns the rendered contentof that file into the current namespace:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre>{% include 'header.html' %}
Body
{% include 'footer.html' %}
</pre></div></td></tr></table>
Included templates have access to the variables of the active context.
If you are using the filesystem loader, the templates are looked for in thepaths defined by it.
You can add additional variables by passing them after the `with` keyword:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre>{# template.html will have access to the variables from the current context and the additional ones provided #}
{% include 'template.html' with {'foo': 'bar'} %}
{% set vars = {'foo': 'bar'} %}
{% include 'template.html' with vars %}
</pre></div></td></tr></table>
You can disable access to the context by appending the `only` keyword:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre>{# only the foo variable will be accessible #}
{% include 'template.html' with {'foo': 'bar'} only %}
</pre></div></td></tr></table>
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre>{# no variables will be accessible #}
{% include 'template.html' only %}
</pre></div></td></tr></table>
Tip
When including a template created by an end user, you should considersandboxing it. More information in the [*Twig for Developers*](#)chapter and in the [*sandbox*](#) tag documentation.
The template name can be any valid Twig expression:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre>{% include some_var %}
{% include ajax ? 'ajax.html' : 'not_ajax.html' %}
</pre></div></td></tr></table>
And if the expression evaluates to a `Twig_Template` object, Twig will use itdirectly:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre>// {% include template %}
$template = $twig->loadTemplate('some_template.twig');
$twig->loadTemplate('template.twig')->display(array('template' => $template));
</pre></div></td></tr></table>
New in version 1.2: The `ignore missing` feature has been added in Twig 1.2.
You can mark an include with `ignore missing` in which case Twig will ignorethe statement if the template to be included does not exist. It has to beplaced just after the template name. Here some valid examples:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre>{% include 'sidebar.html' ignore missing %}
{% include 'sidebar.html' ignore missing with {'foo': 'bar'} %}
{% include 'sidebar.html' ignore missing only %}
</pre></div></td></tr></table>
New in version 1.2: The possibility to pass an array of templates has been added in Twig 1.2.
You can also provide a list of templates that are checked for existence beforeinclusion. The first template that exists will be included:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>{% include ['page_detailed.html', 'page.html'] %}
</pre></div></td></tr></table>
If `ignore missing` is given, it will fall back to rendering nothing if noneof the templates exist, otherwise it will throw an exception.
- Twig
- Introduction
- Installation
- Twig for Template Designers
- Twig for Developers
- Extending Twig
- Twig Internals
- Deprecated Features
- Recipes
- Coding Standards
- Tags
- autoescape
- block
- do
- embed
- extends
- filter
- flush
- for
- from
- if
- import
- include
- macro
- sandbox
- set
- spaceless
- use
- verbatim
- Filters
- abs
- batch
- capitalize
- convert_encoding
- date
- date_modify
- default
- escape
- first
- format
- join
- json_encode
- keys
- last
- length
- lower
- merge
- nl2br
- number_format
- raw
- replace
- reverse
- round
- slice
- sort
- split
- striptags
- title
- trim
- upper
- url_encode
- Functions
- attribute
- block
- constant
- cycle
- date
- dump
- include
- max
- min
- parent
- random
- range
- source
- template_from_string
- Tests
- constant
- defined
- divisible by
- empty
- even
- iterable
- null
- odd
- same as