escape

The escape filter escapes a string using strategies that depend on the context.

エスケープ フィルタは、コンテキストに依存する戦略を使用して文字列をエスケープします。

By default, it uses the HTML escaping strategy:

デフォルトでは、HTML エスケープ戦略を使用します。
1
2
3
<p>
    {{ user.username|escape }}
</p>

For convenience, the e filter is defined as an alias:

便宜上、e フィルターはエイリアスとして定義されています。
1
2
3
<p>
    {{ user.username|e }}
</p>

The escape filter can also be used in other contexts than HTML thanks to an optional argument which defines the escaping strategy to use:

エスケープ フィルタは、使用するエスケープ戦略を定義するオプションの引数のおかげで、HTML 以外のコンテキストでも使用できます。
1
2
3
{{ user.username|e }}
{# is equivalent to #}
{{ user.username|e('html') }}

And here is how to escape variables included in JavaScript code:

JavaScript コードに含まれる変数をエスケープする方法は次のとおりです。
1
2
{{ user.username|escape('js') }}
{{ user.username|e('js') }}

The escape filter supports the following escaping strategies for HTML documents:

エスケープ フィルターは、HTML ドキュメントに対して次のエスケープ戦略をサポートします。
  • html: escapes a string for the HTML body context.
    html: HTML 本文コンテキストの文字列をエスケープします。
  • js: escapes a string for the JavaScript context.
    js: JavaScript コンテキストの文字列をエスケープします。
  • css: escapes a string for the CSS context. CSS escaping can be applied to any string being inserted into CSS and escapes everything except alphanumerics.
    css: CSS コンテキストの文字列をエスケープします。 CSS エスケープは、CSS に挿入される任意の文字列に適用でき、英数字以外のすべてをエスケープします。
  • url: escapes a string for the URI or parameter contexts. This should not be used to escape an entire URI; only a subcomponent being inserted.
    url: URI またはパラメーター コンテキストの文字列をエスケープします。これは、URI 全体をエスケープするために使用しないでください。サブコンポーネントのみが挿入されます。
  • html_attr: escapes a string for the HTML attribute context.
    html_attr: HTML 属性コンテキストの文字列をエスケープします。

Note that doing contextual escaping in HTML documents is hard and choosing the right escaping strategy depends on a lot of factors. Please, read related documentation like the OWASP prevention cheat sheet to learn more about this topic.

HTML ドキュメントでコンテキスト エスケープを行うのは難しく、適切なエスケープ戦略の選択は多くの要因に依存することに注意してください。このトピックの詳細については、OWASP 防止チート シートなどの関連ドキュメントをお読みください。

Note

ノート

Internally, escape uses the PHP native htmlspecialchars function for the HTML escaping strategy.

内部的には、エスケープは HTML エスケープ戦略に PHP ネイティブの htmlspecialchars 関数を使用します。

Caution

注意

When using automatic escaping, Twig tries to not double-escape a variable when the automatic escaping strategy is the same as the one applied by the escape filter; but that does not work when using a variable as the escaping strategy:

自動エスケープを使用する場合、Twig は、自動エスケープ戦略がエスケープ フィルターによって適用される戦略と同じ場合、変数を二重エスケープしないようにします。ただし、変数をエスケープ戦略として使用する場合は機能しません。
1
2
3
4
5
6
{% set strategy = 'html' %}

{% autoescape 'html' %}
    {{ var|escape('html') }}   {# won't be double-escaped #}
    {{ var|escape(strategy) }} {# will be double-escaped #}
{% endautoescape %}

When using a variable as the escaping strategy, you should disable automatic escaping:

変数をエスケープ戦略として使用する場合は、自動エスケープを無効にする必要があります。
1
2
3
4
5
{% set strategy = 'html' %}

{% autoescape 'html' %}
    {{ var|escape(strategy)|raw }} {# won't be double-escaped #}
{% endautoescape %}

Custom Escapers

You can define custom escapers by calling the setEscaper() method on the escaper extension instance. The first argument is the escaper name (to be used in the escape call) and the second one must be a valid PHP callable:

エスケーパー拡張インスタンスで setEscaper() メソッドを呼び出すことにより、カスタム エスケーパーを定義できます。最初の引数は (エスケープ呼び出しで使用される) エスケーパー名で、2 番目の引数は有効な PHP 呼び出し可能オブジェクトでなければなりません:
1
2
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\EscaperExtension::class)->setEscaper('csv', 'csv_escaper');

When called by Twig, the callable receives the Twig environment instance, the string to escape, and the charset.

Twig によって呼び出されると、callable は Twig 環境インスタンス、エスケープする文字列、および文字セットを受け取ります。

Note

ノート

Built-in escapers cannot be overridden mainly because they should be considered as the final implementation and also for better performance.

ビルトイン エスケーパーは、主に最終的な実装と見なされる必要があるため、またパフォーマンスを向上させるためにオーバーライドできません。

Arguments

  • strategy: The escaping strategy
    戦略: エスケープ戦略
  • charset: The string charset
    charset:文字列の文字セット