use

Note

ノート

Horizontal reuse is an advanced Twig feature that is hardly ever needed in regular templates. It is mainly used by projects that need to make template blocks reusable without using inheritance.

水平再利用は高度な Twig 機能であり、通常のテンプレートではほとんど必要ありません。主に、継承を使用せずにテンプレート ブロックを再利用可能にする必要があるプロジェクトで使用されます。

Template inheritance is one of the most powerful features of Twig but it is limited to single inheritance; a template can only extend one other template. This limitation makes template inheritance simple to understand and easy to debug:

テンプレートの継承は Twig の最も強力な機能の 1 つですが、単一の継承に限定されています。テンプレートは、他の 1 つのテンプレートのみを拡張できます。この制限により、テンプレートの継承が理解しやすく、デバッグしやすくなります。
1
2
3
4
{% extends "base.html" %}

{% block title %}{% endblock %}
{% block content %}{% endblock %}

Horizontal reuse is a way to achieve the same goal as multiple inheritance, but without the associated complexity:

水平再利用は、多重継承と同じ目標を達成する方法ですが、関連する複雑さはありません。
1
2
3
4
5
6
{% extends "base.html" %}

{% use "blocks.html" %}

{% block title %}{% endblock %}
{% block content %}{% endblock %}

The use statement tells Twig to import the blocks defined in blocks.html into the current template (it's like macros, but for blocks):

use ステートメントは Twig に、blocks.html で定義されたブロックを現在のテンプレートにインポートするように指示します (マクロに似ていますが、ブロック用です)。
1
2
3
{# blocks.html #}

{% block sidebar %}{% endblock %}

In this example, the use statement imports the sidebar block into the main template. The code is mostly equivalent to the following one (the imported blocks are not outputted automatically):

この例では、use ステートメントがサイドバー ブロックをメイン テンプレートにインポートします。コードは次のものとほとんど同じです (インポートされたブロックは自動的に出力されません)。
1
2
3
4
5
{% extends "base.html" %}

{% block sidebar %}{% endblock %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}

Note

ノート

The use tag only imports a template if it does not extend another template, if it does not define macros, and if the body is empty. But it can use other templates.

use タグは、別のテンプレートを拡張しない場合、マクロを定義しない場合、および本文が空の場合にのみ、テンプレートをインポートします。ただし、他のテンプレートを使用できます。

Note

ノート

Because use statements are resolved independently of the context passed to the template, the template reference cannot be an expression.

use ステートメントはテンプレートに渡されたコンテキストとは無関係に解決されるため、テンプレート参照を式にすることはできません。

The main template can also override any imported block. If the template already defines the sidebar block, then the one defined in blocks.html is ignored. To avoid name conflicts, you can rename imported blocks:

メイン テンプレートは、インポートされたブロックをオーバーライドすることもできます。テンプレートがすでにサイドバー ブロックを定義している場合、blocks.html で定義されているものは無視されます。名前の競合を避けるために、インポートされたブロックの名前を変更できます。
1
2
3
4
5
6
7
{% extends "base.html" %}

{% use "blocks.html" with sidebar as base_sidebar, title as base_title %}

{% block sidebar %}{% endblock %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}

The parent() function automatically determines the correct inheritance tree, so it can be used when overriding a block defined in an imported template:

parent() 関数は正しい継承ツリーを自動的に決定するため、インポートされたテンプレートで定義されたブロックをオーバーライドするときに使用できます。
1
2
3
4
5
6
7
8
9
10
{% extends "base.html" %}

{% use "blocks.html" %}

{% block sidebar %}
    {{ parent() }}
{% endblock %}

{% block title %}{% endblock %}
{% block content %}{% endblock %}

In this example, parent() will correctly call the sidebar block from the blocks.html template.

この例では、parent() は、blocks.html テンプレートからサイドバー ブロックを正しく呼び出します。

Tip

ヒント

Renaming allows you to simulate inheritance by calling the "parent" block:

名前を変更すると、「親」ブロックを呼び出すことで継承をシミュレートできます。
1
2
3
4
5
6
7
{% extends "base.html" %}

{% use "blocks.html" with sidebar as parent_sidebar %}

{% block sidebar %}
    {{ block('parent_sidebar') }}
{% endblock %}

Note

ノート

You can use as many use statements as you want in any given template. If two imported templates define the same block, the latest one wins.

任意のテンプレートで必要な数の use ステートメントを使用できます。2 つのインポートされたテンプレートが同じブロックを定義する場合、最新のテンプレートが優先されます。