Coding Standars
When crafting templates, it's recommended to adhere to the official coding standards outlined below:
Use a single space after the beginning of a delimiter (
{{
,{%
, and{#
) and before the end of a delimiter (}}
,%}
, and#
):{{ foo }} {# comment #} {% if foo %}{% endif %}
With the whitespace control character, avoid any spaces between it and the delimiter:
{{- foo -}} {#- comment -#} {%- if foo -%}{%- endif -%}
Maintain one space before and after these operators: comparison (
==
,!=
,<
,>
,>=
,<=
), math (+
,-
,/
,*
,%
,//
,**
), logic (not
,and
,or
),~
,is
,in
, and ternary (?:
):{{ 1 + 2 }} {{ foo ~ bar }} {{ true ? true : false }}
Insert a single space after the
:
in hashes and,
in arrays and hashes:{{ [1, 2, 3] }} {{ {'foo': 'bar'} }}
Refrain from adding spaces after an opening parenthesis or before a closing parenthesis:
{{ 1 + (2 * 3) }}
Ensure no spaces exist before and after string delimiters:
{{ 'foo' }} {{ "foo" }}
Avoid spaces before and after these operators:
|
,.
,..
,[]
:{{ foo|upper|lower }} {{ user.name }} {{ user[name] }} {% for i in 1..12 %}{% endfor %}
Don't add spaces before and after the parenthesis for filter and function calls:
{{ foo|default('foo') }} {{ range(1..10) }}
For arrays and hashes, no spaces should exist after the opening and before the closing:
{{ [1, 2, 3] }} {{ {'foo': 'bar'} }}
Use lowercase and underscore for variable names:
{% set foo = 'foo' %} {% set foo_bar = 'foo' %}
Maintain consistent indentation inside tags.
{% block foo %} {% if true %} true {% endif %} {% endblock %}
Last updated