How to concatenate strings in twig

stoefln picture stoefln · Oct 9, 2011 · Viewed 368.4k times · Source

Anyone knows how to concatenate strings in twig? I want to do something like:

{{ concat('http://', app.request.host) }}

Answer

Alessandro Desantis picture Alessandro Desantis · Oct 9, 2011

This should work fine:

{{ 'http://' ~ app.request.host }}

To add a filter - like 'trans' - in the same tag use

{{ ('http://' ~ app.request.host) | trans }}

As Adam Elsodaney points out, you can also use string interpolation, this does require double quoted strings:

{{ "http://#{app.request.host}" }}