kaytrance
kaytrance

Reputation: 2757

Split string, then output the first characters of each element

Database returns me an initial_string like Abc 123 & Yolo thing. In twig template I need to get the first characters of each space-delimited character sequence, so that in result to see A1Yt.

I have tried the following:

{% set ug = initial_string|split(" ") %}
{% set UG = "" %}
{% for i  in ug  %}
{% set UG += ug[loop.index-1]|first %}
{% endfor %}
{{ UG }}

but twig does not let me alter UG by using +=.

How can I solve it then? Doing it on server-side is not an option, only in twig template.

Upvotes: 0

Views: 32

Answers (1)

xabbuh
xabbuh

Reputation: 5881

Use the ~ operator to concatenate two strings:

{% set UG = UG ~ ug[loop.index-1]|first %}

Upvotes: 1

Related Questions