{% set url = '/f/' . topic.name . '/' %}
I have some problems getting this to work. Anyone knows how to do this correctly?
Concatenation is done with a ~:
{% set url = '/f/' ~ topic.name ~ '/' %}
Related
I've found it weird to notice that assembling data-attributes as string and placing it on html node results in attribute value double quoted.
If i leave it unquoted:
{% set dataId = '' %}
{% if id is defined %}
{% set dataId = ' ' ~ 'data-id=' ~ id %}
{% endif %}
and put on html node:
<div class="testDiv"{{ dataId }}>
then the data-attribute is correctly quoted.
Is this reliable behavior of twig? Why does it work that way?
It turned out that the browser was fixing quotes for me. To be able to output parameters properly, one haz to:
Include quotes in the string:
{% set dataId = ' ' ~ 'data-id="' ~ id ~ '"' %}
And use raw filter to prevent escaping quote characters:
<div class="testDiv"{{ dataId | raw }}>
Is it possible to use ternary operator in Twig when concatenating one string to another if some condition is true?
This works for me:
{% set a = 'initial' %}
{% if foo == bar %}
{% set a = a ~ ' concatenate' %}
{% endif %}
<p>{{ a }}</p>
But when I try to simplify it like this, it throws an error:
{% set a = 'initial' ~ (foo == bar) ? ' concatenate' : '' %}
<p>{{ a }}</p>
Am I doing something wrong or this simplification is simply not possible in Twig?
due to the order of precedence you'll need to add parentheses, {% set a = 'initial' ~ ((foo == bar) ? ' concatenate' : '') %}
If the 2nd part is empty you can even omit it e.g.
{% set b = 'initial' ~ ((foo == foo) ? ' concatenate') %}
twigfiddle
Is there an equivalent to this in Twig :
<?php
$a = 'hello';
$b = '<h1>'.$a.'</h1>';
echo $b;
?>
I try this but without success :
{% set a = 'hello' %}
{% set b = <h1>{{a}}</h1> %}
I'm new to twig and couldn't find a way to do this.
To concatenate in Twig you need the tilda symbol, ~
Eg.
{% set a = 'hello' %}
{% set b = '<h1>'~a~'</h1>' %}
Or you can use string interpolation
E.g.
{{ "<h1>{a}</h1>" }}
You can concatenate and dump with the raw filter:
{% set a = 'hello' %}
{% set b = '<h1>' ~ a ~ '</h1>' %}
{{ b|raw }}
Here a working solutions
Im trying to create a random string with twig with a set prefix eg myPrefix123. The following simply returns myPrefix:
{% set randomID = 'myPrefix'|format(random(1000)) %}
{% set randomID = 'myPrefix' ~ random(1000) %}
How would you append more data to the same variable in Twig? For example, this is what I'm trying to do in Twig:
var data = "foo";
data += 'bar';
I have figured out that ~ appends strings together in Twig. When I try {% set data ~ 'foo' %} I get an error in Twig.
The ~ operator does not perform assignment, which is the likely cause of the error.
Instead, you need to assign the appended string back to the variable:
{% set data = data ~ 'foo' %}
See also: How to combine two string in twig?
Displaying dynamically in twig
{% for Resp in test.TestRespuestasA %}
{% set name = "preg_A_" ~ Resp.id %}
{% set name_aux = "preg_A_comentario" ~ Resp.id %}
<li>{{ form_row(attribute(form, name)) }}</li>
{% endfor %}
You can also define a custom filter like Liquid's |append filter in your Twig instance which does the same thing.
$loader = new Twig_Loader_Filesystem('./path/to/views/dir');
$twig = new Twig_Environment($loader);
...
...
$twig->addFilter(new Twig_SimpleFilter('append', function($val, $append) {
return $val . $append;
}));
Resulting in the following markup:
{% set pants = 'I\'m wearing stretchy pants!' %}
{% set part2 = ' and they\'re friggin\' comfy!' %}
{% set pants = pants|append(part2) %}
{{ pants }}
{# result: I'm wearing stretchy pants! and they're friggin' comfy! #}
IMHO I find the above sample more intuitive than the ~ combinator, especially when working on a shared codebase where people new to the syntax might get a bit mixed up.