Delimiter Trims

Delimiter whitespace trim dashes {%-, -%}, {{- and -}} control. This rule can be used for handling trim - application of delimiter expressions in Liquid tag and output type tokens. This is a Liquid specific formatting rule which defaults to using preserve when no option has been specified. The recommended option to use is tags or never.

This rule will not touch Liquid tokens encapsulated within strings, e.g: "{{ foo }}". Tags which exist in string values or those contained between quotation characters are left intact.


👍

preserve

The delimiterTrims rule is set to preserve by default and delimiter trims applied Liquid tokens intact. In the sample all occurrences of whitespace trims will be preserved.

{% if condition -%}
  {{- foo }}
  {{ bar -}}
{%- endif -%}
{% if condition -%}
  {{- foo }}
  {{ bar -}}
{%- endif -%}

👍

tags

When the delimiterTrims rule is set to tags then Liquid tokens using {% and %} delimiters will have trims applied. This rule will leave output token ({{ and }}) delimiter trims intact. In the sample all tag occurrences have trims applied, whereas output token types are preserved.

{% if condition -%}

  {% # Trims will be inserted here %}
  {% render 'snippet' %}

  {% # These output tag type delimiter trims are preserved %}
  {{- foo }}
  {{ bar -}}

{% endif %}
{%- if condition -%}

  {% # Trims will be inserted here %}
  {%- render 'snippet' -%}

  {% # These output tag type delimiter trims are preserved %}
  {{ foo }}
  {{ bar }}

{%- endif -%}

👎

outputs

When the delimiterTrims rule is set to outputs then Liquid tokens using {{ and }} delimiters will have trims applied. This rule will leave tag type token {% and %} delimiter trims intact. In the sample the if, render and endif tag delimiter trims will be preserved but the {{ foo }}and {{ bar }} output tags will have trims inserted.

{%- if condition -%}

  {% # Trim application will be preserved %}
  {% render 'snippet' -%}

  {% # The output tags will have trims applied %}
  {{ foo }}
  {{ bar }}

{%- endif %}
{% if condition %}

  {% # Trim application will be preserved %}
  {% render 'snippet' %}

  {% # The output tags will have trims applied %}
  {{- foo -}}
  {{- bar -}}

{% endif %}

👎

never

When the delimiterTrims rule is set to never then all occurrence’s trim dash delimiters will be stripped from Liquid tag and output token types. In the sample, all - delimiters expressions are removed.

{%- if condition -%}

  {% # All trims will be stripped %}

  {%- render 'snippet' -%}

  {{- foo -}}
  {{- bar -}}
  {{- baz -}}

{%- endif %}
{% if condition %}

  {% # All trims will be stripped %}

  {% render 'snippet' %}

  {{ foo }}
  {{ bar }}
  {{ baz }}

{% endif %}

😳

always

When the delimiterTrims rule is set to always then all Liquid delimiters will have trims applied. Maybe avoid using this option unless you are sure the resulting render is as you intend. Keep in mind, trims are a minor performance hit in Liquid. In the sample, all {{, {%, }} and %} delimiters have trims inserted.

{% if condition %}

  {% # Trims will be applied to all tokens %}

  {% render 'snippet' %}

  {{ foo }}
  {{ bar }}
  {{ baz }}

{% endif %}
{%- if condition -%}

  {% # Trims will be applied to all tokens %}

  {%- render 'snippet' -%}

  {{- foo -}}
  {{- bar -}}
  {{- baz -}}

{%- endif -%}

👎

multiline

When the delimiterTrims rule is set to multiline trims will be applied to tags and output type tokens when the internal structure spans multiple lines. This typically occurs on tokens which contain several filters, arguments or control conditions. Both the opening and closing delimiters will apply trims.

{%
  if condition == assertion
  or condition == expectation
  or something == comparison
%}

  {{
    object.prop
    | param_1: true
    | param_2: 1000
    | param_3:
      arg_1: 'value',
      arg_2: 2000,
      arg_3: false,
      arg_4: true,
      arg_5: 2000,
      arg_6: nil
    | param_4: true
    | param_5: 1000
  }}

{% endif %}
{%-
  if condition == assertion
  or condition == expectation
  or something == comparison
-%}

  {{-
    object.prop
    | param_1: true
    | param_2: 1000
    | param_3:
      arg_1: 'value',
      arg_2: 2000,
      arg_3: false,
      arg_4: true,
      arg_5: 2000,
      arg_6: nil
    | param_4: true
    | param_5: 1000
  -}}

{% endif %}