Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concatenate strings in twig

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

{{ concat('http://', app.request.host) }} 
like image 535
stoefln Avatar asked Oct 09 '11 14:10

stoefln


People also ask

What is tilde in twig?

#Using the ~ (Tilde) Operator The tilde character concatenates all operands (strings and/or variables) into a single string. For example: {{ foo ~ ' ' ~ bar ~ '!'


2 Answers

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}" }} 
like image 103
Alessandro Desantis Avatar answered Sep 17 '22 16:09

Alessandro Desantis


Also a little known feature in Twig is string interpolation:

{{ "http://#{app.request.host}" }} 
like image 40
Adam Elsodaney Avatar answered Sep 18 '22 16:09

Adam Elsodaney