]>
Commit | Line | Data |
---|---|---|
4f5b44bd NL |
1 | Coding Standards |
2 | ================ | |
3 | ||
4 | When writing Twig templates, we recommend you to follow these official coding | |
5 | standards: | |
6 | ||
7 | * Put one (and only one) space after the start of a delimiter (``{{``, ``{%``, | |
8 | and ``{#``) and before the end of a delimiter (``}}``, ``%}``, and ``#}``): | |
9 | ||
10 | .. code-block:: jinja | |
11 | ||
12 | {{ foo }} | |
13 | {# comment #} | |
14 | {% if foo %}{% endif %} | |
15 | ||
16 | When using the whitespace control character, do not put any spaces between | |
17 | it and the delimiter: | |
18 | ||
19 | .. code-block:: jinja | |
20 | ||
21 | {{- foo -}} | |
22 | {#- comment -#} | |
23 | {%- if foo -%}{%- endif -%} | |
24 | ||
25 | * Put one (and only one) space before and after the following operators: | |
26 | comparison operators (``==``, ``!=``, ``<``, ``>``, ``>=``, ``<=``), math | |
27 | operators (``+``, ``-``, ``/``, ``*``, ``%``, ``//``, ``**``), logic | |
28 | operators (``not``, ``and``, ``or``), ``~``, ``is``, ``in``, and the ternary | |
29 | operator (``?:``): | |
30 | ||
31 | .. code-block:: jinja | |
32 | ||
33 | {{ 1 + 2 }} | |
34 | {{ foo ~ bar }} | |
35 | {{ true ? true : false }} | |
36 | ||
37 | * Put one (and only one) space after the ``:`` sign in hashes and ``,`` in | |
38 | arrays and hashes: | |
39 | ||
40 | .. code-block:: jinja | |
41 | ||
42 | {{ [1, 2, 3] }} | |
43 | {{ {'foo': 'bar'} }} | |
44 | ||
45 | * Do not put any spaces after an opening parenthesis and before a closing | |
46 | parenthesis in expressions: | |
47 | ||
48 | .. code-block:: jinja | |
49 | ||
50 | {{ 1 + (2 * 3) }} | |
51 | ||
52 | * Do not put any spaces before and after string delimiters: | |
53 | ||
54 | .. code-block:: jinja | |
55 | ||
56 | {{ 'foo' }} | |
57 | {{ "foo" }} | |
58 | ||
59 | * Do not put any spaces before and after the following operators: ``|``, | |
60 | ``.``, ``..``, ``[]``: | |
61 | ||
62 | .. code-block:: jinja | |
63 | ||
64 | {{ foo|upper|lower }} | |
65 | {{ user.name }} | |
66 | {{ user[name] }} | |
67 | {% for i in 1..12 %}{% endfor %} | |
68 | ||
69 | * Do not put any spaces before and after the parenthesis used for filter and | |
70 | function calls: | |
71 | ||
72 | .. code-block:: jinja | |
73 | ||
74 | {{ foo|default('foo') }} | |
75 | {{ range(1..10) }} | |
76 | ||
77 | * Do not put any spaces before and after the opening and the closing of arrays | |
78 | and hashes: | |
79 | ||
80 | .. code-block:: jinja | |
81 | ||
82 | {{ [1, 2, 3] }} | |
83 | {{ {'foo': 'bar'} }} | |
84 | ||
85 | * Use lower cased and underscored variable names: | |
86 | ||
87 | .. code-block:: jinja | |
88 | ||
89 | {% set foo = 'foo' %} | |
90 | {% set foo_bar = 'foo' %} | |
91 | ||
92 | * Indent your code inside tags (use the same indentation as the one used for | |
93 | the main language of the file): | |
94 | ||
95 | .. code-block:: jinja | |
96 | ||
97 | {% block foo %} | |
98 | {% if true %} | |
99 | true | |
100 | {% endif %} | |
101 | {% endblock %} |