💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# `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-&gt;loadTemplate('some_template.twig'); $twig-&gt;loadTemplate('template.twig')-&gt;display(array('template' =&gt; $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.