Search code examples
variablestwig

Concatenate multiple twig variables in one bracket


I want to concatenate 3 variables in one bracket, like PHP.

i.e

In PHP:

getimagesize($path.$image)

We can concatenate variables with dot notation. How can I reproduce this in twig?

{{ path.key }} isn't working as intended.


Solution

  • Use the tilde character ~ to concatenate string and/or variables.

    ~: Converts all operands into strings and concatenates them. {{ "Hello " ~ name ~ "!" }} would return (assuming name is 'John') Hello John!.

    Read about it in the documentation.