macro

Macros are comparable with functions in regular programming languages. They are useful to reuse template fragments to not repeat yourself.

マクロは、通常のプログラミング言語の関数と同等です。テンプレートのフラグメントを再利用して、同じことを繰り返さないようにするのに役立ちます。

Macros are defined in regular templates.

マクロは通常のテンプレートで定義されます。

Imagine having a generic helper template that define how to render HTML forms via macros (called forms.html):

マクロ (forms.html と呼ばれる) を介して HTML フォームをレンダリングする方法を定義する汎用ヘルパー テンプレートがあるとします。
1
2
3
4
5
6
7
{% macro input(name, value, type = "text", size = 20) %}
    <input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}"/>
{% endmacro %}

{% macro textarea(name, value, rows = 10, cols = 40) %}
    <textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols }}">{{ value|e }}</textarea>
{% endmacro %}

Each macro argument can have a default value (here text is the default value for type if not provided in the call).

各マクロ引数はデフォルト値を持つことができます (ここで、呼び出しで指定されていない場合、テキストは type のデフォルト値です)。

Macros differ from native PHP functions in a few ways:

マクロは、いくつかの点でネイティブ PHP 関数と異なります。
  • Arguments of a macro are always optional.
    マクロの引数は常にオプションです。
  • If extra positional arguments are passed to a macro, they end up in the special varargs variable as a list of values.
    追加の位置引数がマクロに渡される場合、それらは値のリストとして特別な varargs 変数になります。

But as with PHP functions, macros don't have access to the current template variables.

しかし、PHP 関数と同様に、マクロは現在のテンプレート変数にアクセスできません。

Tip

ヒント

You can pass the whole context as an argument by using the special _context variable.

special_context 変数を使用して、コンテキスト全体を引数として渡すことができます。

Importing Macros

There are two ways to import macros. You can import the complete template containing the macros into a local variable (via the import tag) or only import specific macros from the template (via the from tag).

マクロをインポートするには 2 つの方法があります。マクロを含む完全なテンプレートをローカル変数にインポートする (import タグを使用) か、テンプレートから特定のマクロのみをインポートする (from タグを使用) ことができます。

To import all macros from a template into a local variable, use the import tag:

テンプレートからすべてのマクロをローカル変数にインポートするには、importtag を使用します。
1
{% import "forms.html" as forms %}

The above import call imports the forms.html file (which can contain only macros, or a template and some macros), and import the macros as items of the forms local variable.

上記の import 呼び出しは、forms.html ファイル (マクロのみ、またはテンプレートといくつかのマクロを含むことができます) をインポートし、マクロを forms ローカル変数の項目としてインポートします。

The macros can then be called at will in the current template:

マクロは、現在のテンプレートで自由に呼び出すことができます。
1
2
<p>{{ forms.input('username') }}</p>
<p>{{ forms.input('password', null, 'password') }}</p>

Alternatively you can import names from the template into the current namespace via the from tag:

または、 from タグを使用して、テンプレートから現在の名前空間に名前をインポートできます。
1
2
3
4
{% from 'forms.html' import input as input_field, textarea %}

<p>{{ input_field('password', '', 'password') }}</p>
<p>{{ textarea('comment') }}</p>

Tip

ヒント

When macro usages and definitions are in the same template, you don't need to import the macros as they are automatically available under the special _self variable:

マクロの使用法と定義が同じテンプレートにある場合、マクロは special_self 変数で自動的に使用可能になるため、インポートする必要はありません。
1
2
3
4
5
<p>{{ _self.input('password', '', 'password') }}</p>

{% macro input(name, value, type = "text", size = 20) %}
    <input type="{{ type }}" name="{{ name }}" value="{{ value|e }}" size="{{ size }}"/>
{% endmacro %}

Macros Scoping

The scoping rules are the same whether you imported macros via import or from.

import または from を介してマクロをインポートした場合でも、スコープ規則は同じです。

Imported macros are always local to the current template. It means that macros are available in all blocks and other macros defined in the current template, but they are not available in included templates or child templates; you need to explicitly re-import macros in each template.

インポートされたマクロは、常に現在のテンプレートに対してローカルです。これは、現在のテンプレートで定義されているすべてのブロックおよびその他のマクロでマクロを使用できますが、含まれているテンプレートまたは子テンプレートではマクロを使用できないことを意味します。各テンプレートでマクロを明示的に再インポートする必要があります。

Imported macros are not available in the body of embed tags, you need to explicitly re-import macros inside the tag.

インポートされたマクロは埋め込みタグの本文では使用できません。タグ内でマクロを明示的に再インポートする必要があります。

When calling import or from from a block tag, the imported macros are only defined in the current block and they override macros defined at the template level with the same names.

ブロック タグから import または from を呼び出すと、インポートされたマクロは現在のブロックでのみ定義され、テンプレート レベルで定義された同じ名前のマクロをオーバーライドします。

When calling import or from from a macro tag, the imported macros are only defined in the current macro and they override macros defined at the template level with the same names.

import または from をマクロ タグから呼び出すと、インポートされたマクロは現在のマクロでのみ定義され、テンプレート レベルで定義された同じ名前のマクロを上書きします。

Checking if a Macro is defined

You can check if a macro is defined via the defined test:

定義されたテストを介してマクロが定義されているかどうかを確認できます。
1
2
3
4
5
6
7
8
9
10
11
{% import "macros.twig" as macros %}

{% from "macros.twig" import hello %}

{% if macros.hello is defined -%}
    OK
{% endif %}

{% if hello is defined -%}
    OK
{% endif %}

Named Macro End-Tags

Twig allows you to put the name of the macro after the end tag for better readability (the name after the endmacro word must match the macro name):

Twig では、読みやすくするために終了タグの後にマクロの名前を付けることができます (endmacro ワードの後の名前はマクロ名と一致する必要があります)。
1
2
3
{% macro input() %}
    ...
{% endmacro input %}