autoescape

Whether automatic escaping is enabled or not, you can mark a section of a template to be escaped or not by using the autoescape tag:

自動エスケープが有効になっているかどうかに関係なく、 autoescape タグを使用して、テンプレートのセクションをエスケープするかどうかをマークできます。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{% autoescape %}
    Everything will be automatically escaped in this block
    using the HTML strategy
{% endautoescape %}

{% autoescape 'html' %}
    Everything will be automatically escaped in this block
    using the HTML strategy
{% endautoescape %}

{% autoescape 'js' %}
    Everything will be automatically escaped in this block
    using the js escaping strategy
{% endautoescape %}

{% autoescape false %}
    Everything will be outputted as is in this block
{% endautoescape %}

When automatic escaping is enabled everything is escaped by default except for values explicitly marked as safe. Those can be marked in the template by using the raw filter:

自動エスケープが有効になっている場合、明示的に安全であるとマークされている値を除いて、デフォルトですべてがエスケープされます。これらは、raw フィルターを使用してテンプレートでマークできます。
1
2
3
{% autoescape %}
    {{ safe_value|raw }}
{% endautoescape %}

Functions returning template data (like macros and parent) always return safe markup.

テンプレート データを返す関数 (マクロや親など) は、常に安全なマークアップを返します。

Note

ノート

Twig is smart enough to not escape an already escaped value by the escape filter.

Twig は、エスケープ フィルターによって既にエスケープされた値をエスケープしないほどスマートです。

Note

ノート

Twig does not escape static expressions:

Twig は静的な式をエスケープしません:
1
2
3
{% set hello = "<strong>Hello</strong>" %}
{{ hello }}
{{ "<strong>world</strong>" }}

Will be rendered "<strong>Hello</strong> world".

「Hello world」と表示されます。

Note

ノート

The chapter Twig for Developers gives more information about when and how automatic escaping is applied.

Twig for Developers の章では、自動エスケープがいつ、どのように適用されるかについての詳細を説明しています。