include

The include function returns the rendered content of a template:

include 関数は、レンダリングされたテンプレートのコンテンツを返します。
1
2
{{ include('template.html') }}
{{ include(some_var) }}

Included templates have access to the variables of the active context.

含まれているテンプレートは、アクティブなコンテキストの変数にアクセスできます。

If you are using the filesystem loader, the templates are looked for in the paths defined by it.

ファイルシステムローダーを使用している場合、テンプレートはそれによって定義されたパスで検索されます。

The context is passed by default to the template but you can also pass additional variables:

コンテキストはデフォルトでテンプレートに渡されますが、追加の変数を渡すこともできます:
1
2
{# template.html will have access to the variables from the current context and the additional ones provided #}
{{ include('template.html', {foo: 'bar'}) }}

You can disable access to the context by setting with_context to false:

with_context を false に設定することで、コンテキストへのアクセスを無効にできます。
1
2
{# only the foo variable will be accessible #}
{{ include('template.html', {foo: 'bar'}, with_context = false) }}
1
2
{# no variables will be accessible #}
{{ include('template.html', with_context = false) }}

And if the expression evaluates to a \Twig\Template or a \Twig\TemplateWrapper instance, Twig will use it directly:

また、式が \Twig\Template または \Twig\TemplateWrapper インスタンスに評価される場合、Twig はそれを直接使用します。
1
2
3
4
5
// {{ include(template) }}

$template = $twig->load('some_template.twig');

$twig->display('template.twig', ['template' => $template]);

When you set the ignore_missing flag, Twig will return an empty string if the template does not exist:

ignore_missing フラグを設定すると、テンプレートが存在しない場合、Twig は空の文字列を返します。
1
{{ include('sidebar.html', ignore_missing = true) }}

You can also provide a list of templates that are checked for existence before inclusion. The first template that exists will be rendered:

含める前に存在をチェックするテンプレートのリストを提供することもできます。存在する最初のテンプレートがレンダリングされます。
1
{{ include(['page_detailed.html', 'page.html']) }}

If ignore_missing is set, it will fall back to rendering nothing if none of the templates exist, otherwise it will throw an exception.

ignore_missing が設定されている場合、テンプレートが存在しない場合は何もレンダリングせず、存在しない場合は例外がスローされます。

When including a template created by an end user, you should consider sandboxing it:

エンド ユーザーが作成したテンプレートを含める場合は、サンドボックス化を検討する必要があります。
1
{{ include('page.html', sandboxed = true) }}

Arguments

  • template: The template to render
    template: レンダリングするテンプレート
  • variables: The variables to pass to the template
    変数:テンプレートに渡す変数
  • with_context: Whether to pass the current context variables or not
    with_context: 現在のコンテキスト変数を渡すかどうか
  • ignore_missing: Whether to ignore missing templates or not
    ignore_missing: 欠落しているテンプレートを無視するかどうか
  • sandboxed: Whether to sandbox the template or not
    sandboxed:テンプレートをサンドボックス化するかどうか