Post

Twig Filter Cheatsheet

just get the value from a field

1
2
    {{ content.field_description|field_value }}

Remove spaces from text

1
{{ "it would be great to combine these words" | replace({' ': ''}) }}

Remove spaces and make it lowercase

This is good for dynamically creating variable names

1
{% set my_id = some_other_var | replace({' ': ''}) | lower %}

Remove some dots from a string

1
{{ myString|replace({'.': ''}) }}

Batch

Use the batch filter when you need to turn a single list of 6 items into a two column list of 6 items (3 per column)

1
2
3
4
5
6
7
8
9
        {% for column in tab_links|batch(3, 'No item') %}
          <div class="col-12 col-sm-5">
            {% for item in column %}
                <div class="link-item">{{ item }}</div>
            {% endfor %}
          </div>
        {% endfor %}

This post is licensed under CC BY 4.0 by the author.