{% set url = '/f/' . topic.name . '/' %}
I have some problems getting this to work. Anyone knows how to do this correctly?
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.
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!
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.
Concatenation is done with a ~
:
{% set url = '/f/' ~ topic.name ~ '/' %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With