Twig is arguably the most powerful, modern and easy to use templating engine in PHP.
When using twig, sooner or later, you will run into the situation where you need to join two strings (concatenation) or evaluate an expression in strings (interpolation). Please consider the following
Concatenate Strings In Twig
To join two strings in Twig, you can use the tilde ~ operator as follows
{{ "Welcome Mr " ~ name }}
This will give the output Welcome Mr JohnDoe if the value of the name variable is JohnDoe.
You can play with this exact example on the twig playground.
Interpolating Strings In Twig
Sometimes you will desire to evaluate an expression and embed the result in a string, this is called interpolation, consider the example below where we will like to display the total sales of two months and display it.
{{ "Jan & Feb Sales = #{jan_sales + feb_sales}"}}
This will give the output “Jan & Feb Sales = 51” if the values of jan_sales and feb_sales are 28 & 23 respectively.
You can play with this exact example on the twig playground.