05.10.2026 | Pascal Crott

How to add an element to an array at the first position in Twig

add-to-first-position.twig
{# The elements array we want to add a '0' to at the beginning. #}
{% set elements = ['1', '2', '3'] %}
{# Working: Merge elements to a new array and override the variable. The result will be ['0', '1', '2', '3']. #}
{% set elements = ['0']|merge(elements) %}
{# Not working: The result will be ['1', '2', '3', '0']. #}
{% set elements = elements|merge(['0']) %}

Issue:

When using the merge function in Twig, the new element is appended to the array. However, we would like the new element to prepend the existing array.

Solution:

Create a new array and append the original array to it.

Weitere DevBits

13.11.2025 | Lothar Ferreira Neumann

Use ternary statements in Twig

ternary-example.html.twig
{# Checks if a URL exists and passes it to the component. Otherwise, sets it to false. Works with primitive data types. #}
{% set link = url ? { url: url } : false %}

{# Checks if the media object is not empty and passes it to the component. Otherwise, sets it to false. Suitable for complex data types like objects. #}
{% set media = media is not empty ? { media: media } : false %}
23.04.2026 | Lothar Ferreira Neumann

Add custom text to gin_login with Form Decorator

We will use the form_decorator module to alter the output of the gin_login module to look something like this:
 

An example what the result looks like when using gin_login_text.

 

This DevBit provides a step-by-step tutorial how to get there.