Coding Standards

When writing Twig templates, we recommend you to follow these official coding standards:

Twig テンプレートを作成するときは、次の公式コーディング標準に従うことをお勧めします。
  • Put one (and only one) space after the start of a delimiter ({{, {%, and {#) and before the end of a delimiter (}}, %}, and #}):

    区切り文字 ({{、{%、および {#) の開始後、および区切り文字 (}}、%}、および #}) の終了前に 1 つ (および 1 つだけ) のスペースを入れます。
    1
    2
    3
    {{ foo }}
    {# comment #}
    {% if foo %}{% endif %}

    When using the whitespace control character, do not put any spaces between it and the delimiter:

    空白制御文字を使用する場合は、区切り文字との間にスペースを入れないでください。
    1
    2
    3
    {{- foo -}}
    {#- comment -#}
    {%- if foo -%}{%- endif -%}
  • Put one (and only one) space before and after the following operators: comparison operators (==, !=, <, >, >=, <=), math operators (+, -, /, *, %, //, **), logic operators (not, and, or), ~, is, in, and the ternary operator (?:):

    次の演算子の前後に 1 つ (1 つだけ) スペースを入れてください: 比較演算子 (==, !=, , >=,
    1
    2
    3
    {{ 1 + 2 }}
    {{ foo ~ bar }}
    {{ true ? true : false }}
  • Put one (and only one) space after the : sign in hashes and , in arrays and hashes:

    : 記号の後に 1 つ (そして 1 つだけ) スペースを入れます。
    1
    2
    {{ [1, 2, 3] }}
    {{ {'foo': 'bar'} }}
  • Do not put any spaces after an opening parenthesis and before a closing parenthesis in expressions:

    式の開き括弧の後と閉じ括弧の前にスペースを入れないでください。
    1
    {{ 1 + (2 * 3) }}
  • Do not put any spaces before and after string delimiters:

    文字列の区切り記号の前後にスペースを入れないでください。
    1
    2
    {{ 'foo' }}
    {{ "foo" }}
  • Do not put any spaces before and after the following operators: |, ., .., []:

    次の演算子の前後にスペースを入れないでください: |、.、..、[]:
    1
    2
    3
    4
    {{ foo|upper|lower }}
    {{ user.name }}
    {{ user[name] }}
    {% for i in 1..12 %}{% endfor %}
  • Do not put any spaces before and after the parenthesis used for filter and function calls:

    フィルターと関数の呼び出しに使用される括弧の前後にスペースを入れないでください。
    1
    2
    {{ foo|default('foo') }}
    {{ range(1..10) }}
  • Do not put any spaces before and after the opening and the closing of arrays and hashes:

    配列とハッシュの開始と終了の前後にスペースを入れないでください。
    1
    2
    {{ [1, 2, 3] }}
    {{ {'foo': 'bar'} }}
  • Use lower cased and underscored variable names:

    小文字と下線付きの変数名を使用します。
    1
    2
    {% set foo = 'foo' %}
    {% set foo_bar = 'foo' %}
  • Indent your code inside tags (use the same indentation as the one used for the target language of the rendered template):

    タグ内のコードをインデントします (レンダリングされたテンプレートのターゲット言語に使用されるものと同じインデントを使用します)。
    1
    2
    3
    4
    5
    {% block foo %}
        {% if true %}
            true
        {% endif %}
    {% endblock %}