Best Practices

When writing your template code we recommend that you follow these best practices.

Indent your code

Indent your code with a tab inside tags. This improves readability. Within the editors in BranchCMS a tag equals 4 spaces.

{% if category.posts %}
    {% for post in category.posts %}
        <p><a href="{{ post.url }}">{{ post.postTitle }}</a></p>
    {% endfor %}
{% endif %}

Delimiter spacing

Put one (and only one) space after the start of a delimiter ( {{, {%, and {# ) and before the end of a delimiter ( }}, %}, and #} )

{{ variable }}
{% if variable %}
{% endif %}
{# comment #}

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

{{- variable -}}
{%- if variable -%}{%- endif -%}
{#- comment -#}

Operator spacing

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 + 2 }}
{{ foo ~ bar }}
{% if variable is not null %}
{{ true ? true : false }}

Do not put any spaces before or after ...

{% for i in 1..10 %}{% endfor %}

Array and hash spacing

Put one (and only one) space after the : sign in hashes and , in arrays and hashes.

{{ [1, 2, 3] }}
{{ {'foo': 'bar', 'foo2': 'bar2'} }}

Do not put any spaces before the opening and closing of arrays and hashes.

{% set array = [1, 2, 3] %}
{% set hash = {'foo': 'bar' } %}

Array, hash and range operator spacing

Do not put any spaces before or after .,  or [ ].

{{ profile.firstName }}
{{ profile[firstName] }}

Parenthesis spacing

Do not put any spaces after an opening parenthesis and before a closing parenthesis in expressions.

{{ 1 + (5 * 3) }}

Do not put any spaces before and after the parenthesis used for filter and function calls.

{{ variable|truncate(100) }}
{{ range(1, 10) }}

String delimiter spacing

Do not put spaces before or after string delimiters (i.e quotes ' " ) unless you intend to include a literal space in the string.

{{ "string" }}
{{ 'string' }}

{{ ' string' }} would include a space before "string".

Filter spacing

Do not put any spaces before or after the filter ( | ) operator.

{{ variable|upper|lower }}
{{ variable|truncate(100) }}