Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate multiple twig variables in one bracket

Tags:

variables

twig

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.

like image 286
Muhammet Arslan Avatar asked Dec 22 '12 21:12

Muhammet Arslan


People also ask

How do you concatenate in twig?

Twig provides two ways to combine strings together: You can concatenate them using the tilde ( ~ ) operator, or you can inject a string into the middle of another string using string interpolation.

Can you concatenate variables?

In JavaScript, we can assign strings to a variable and use concatenation to combine the variable to another string. To concatenate a string, you add a plus sign+ between the strings or string variables you want to connect.

How do I concatenate two variables in PowerShell?

In PowerShell, string concatenation is primarily achieved by using the “+” operator. There are also other ways like enclosing the strings inside double quotes, using a join operator, or using the -f operator.

What is the concatenation of two or more strings?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs.


1 Answers

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.

like image 60
mattsches Avatar answered Oct 02 '22 14:10

mattsches