Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig set a value from a string and variable

Tags:

twig

{% set url = '/f/' . topic.name . '/' %}

I have some problems getting this to work. Anyone knows how to do this correctly?

like image 786
Jhonne Avatar asked Sep 03 '12 04:09

Jhonne


People also ask

How do I concatenate strings and variables 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. String interpolation only works in double-quoted strings.

What is tilde in twig?

The tilde character concatenates all operands (strings and/or variables) into a single string. For example: {{ foo ~ ' ' ~ bar ~ '!' }} // output: hello world! {{ foo|upper ~ ' ' ~ bar ~ '!' }} // output: HELLO world!

Can you use twig in Javascript?

Now that your JS code lives alongside Twig, you can easily transfer Twig variables into Javascript. For example: alert({{twig_variable|json_encode}}); From Twig point of view, JS code is just text string - that's why we can just concate Twig variables with text that make up JS code.


1 Answers

Concatenation is done with a ~:

{% set url = '/f/' ~ topic.name ~ '/' %}
like image 182
Thomas K Avatar answered Sep 28 '22 02:09

Thomas K