Symfony Twig Concat String

Concatenating a string using Twig is pretty simple, if you know what you’re doing.

In my first instance in Twig string concatenation, I wanted to dynamically load some assets based on the route parameters.

With my solution, I ended doing something like this to load a stylesheet, for instance.

<link rel="stylesheet" type="text/css" media="all" href="{{ asset('bundles/hack3r3dradio/css/' ~ player ~ '.css') }}" />

I’m populating that player variable in my controller. The tilde is the concatenation character in Twig. In PHP it’s a period. In Javascript it’s a plus-sign. In Twig it’s a tilde.

Share Button