# Coding Standards
When writing Twig templates, we recommend you to follow these official codingstandards:
-
Put one (and only one) space after the start of a delimiter (`{{`, `{%`,and `{#`) and before the end of a delimiter (`}}`, `%}`, and `#}`):
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre>{{ foo }}
{# comment #}
{% if foo %}{% endif %}
</pre></div></td></tr></table>
When using the whitespace control character, do not put any spaces betweenit and the delimiter:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3</pre></div></td><td class="code"><div class="highlight"><pre>{{- foo -}}
{#- comment -#}
{%- if foo -%}{%- endif -%}
</pre></div></td></tr></table>
-
Put one (and only one) space before and after the following operators:comparison operators (`==`, `!=`, `<`, `>`, `>=`, `<=`), mathoperators (`+`, `-`, `/`, `*`, `%`, `//`, `**`), logicoperators (`not`, `and`, `or`), `~`, `is`, `in`, and the ternaryoperator (`?:`):
~~~
{{ 1 + 2 }}
{{ foo ~ bar }}
{{ true ? true : false }}
~~~
-
Put one (and only one) space after the `:` sign in hashes and `,` inarrays and hashes:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre>{{ [1, 2, 3] }}
{{ {'foo': 'bar'} }}
</pre></div></td></tr></table>
-
Do not put any spaces after an opening parenthesis and before a closingparenthesis in expressions:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td class="code"><div class="highlight"><pre>{{ 1 + (2 * 3) }}
</pre></div></td></tr></table>
-
Do not put any spaces before and after string delimiters:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre>{{ 'foo' }}
{{ "foo" }}
</pre></div></td></tr></table>
-
Do not put any spaces before and after the following operators: `|`,`.`, `..`, `[]`:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4</pre></div></td><td class="code"><div class="highlight"><pre>{{ foo|upper|lower }}
{{ user.name }}
{{ user[name] }}
{% for i in 1..12 %}{% endfor %}
</pre></div></td></tr></table>
-
Do not put any spaces before and after the parenthesis used for filter andfunction calls:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre>{{ foo|default('foo') }}
{{ range(1..10) }}
</pre></div></td></tr></table>
-
Do not put any spaces before and after the opening and the closing of arraysand hashes:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre>{{ [1, 2, 3] }}
{{ {'foo': 'bar'} }}
</pre></div></td></tr></table>
-
Use lower cased and underscored variable names:
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2</pre></div></td><td class="code"><div class="highlight"><pre>{% set foo = 'foo' %}
{% set foo_bar = 'foo' %}
</pre></div></td></tr></table>
-
Indent your code inside tags (use the same indentation as the one used forthe target language of the rendered template):
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre>1
2
3
4
5</pre></div></td><td class="code"><div class="highlight"><pre>{% block foo %}
{% if true %}
true
{% endif %}
{% endblock %}
</pre></div></td></tr></table>
- Twig
- Introduction
- Installation
- Twig for Template Designers
- Twig for Developers
- Extending Twig
- Twig Internals
- Deprecated Features
- Recipes
- Coding Standards
- Tags
- autoescape
- block
- do
- embed
- extends
- filter
- flush
- for
- from
- if
- import
- include
- macro
- sandbox
- set
- spaceless
- use
- verbatim
- Filters
- abs
- batch
- capitalize
- convert_encoding
- date
- date_modify
- default
- escape
- first
- format
- join
- json_encode
- keys
- last
- length
- lower
- merge
- nl2br
- number_format
- raw
- replace
- reverse
- round
- slice
- sort
- split
- striptags
- title
- trim
- upper
- url_encode
- Functions
- attribute
- block
- constant
- cycle
- date
- dump
- include
- max
- min
- parent
- random
- range
- source
- template_from_string
- Tests
- constant
- defined
- divisible by
- empty
- even
- iterable
- null
- odd
- same as