ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# `include` New in version 1.12: The `include` function was added in Twig 1.12. The `include` function returns the rendered content of a template: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2</pre></div></td><td class="code"><div class="highlight"><pre>{{ include('template.html') }} {{ include(some_var) }} </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. The context is passed by default to the template but you can also passadditional variables: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1 2</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', {foo: 'bar'}) }} </pre></div></td></tr></table> You can disable access to the context by setting `with_context` to`false`: <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', {foo: 'bar'}, with_context = false) }} </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', with_context = false) }} </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> When you set the `ignore_missing` flag, Twig will return an empty string ifthe template does not exist: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>{{ include('sidebar.html', ignore_missing = true) }} </pre></div></td></tr></table> You can also provide a list of templates that are checked for existence beforeinclusion. The first template that exists will be rendered: <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 set, it will fall back to rendering nothing if noneof the templates exist, otherwise it will throw an exception. When including a template created by an end user, you should considersandboxing it: <table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>{{ include('page.html', sandboxed = true) }} </pre></div></td></tr></table> ### Arguments - `template`: The template to render - `variables`: The variables to pass to the template - `with_context`: Whether to pass the current context variables or not - `ignore_missing`: Whether to ignore missing templates or not - `sandboxed`: Whether to sandbox the template or not