Create a random string in twig with prefix? - twig

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) %}

Related

Set Twig object property name using variable

I need to set an object property name dynamically in Twig:
{% set featureId = feature.id %}
{% set gridEnabled = gridEnabled|merge({featureId: true}) %}
But that sets "featureId" as a property of gridEnabled. Is there a way to tell Twig that featureId is a variable? I'm surprised it interprets that as a string without quotes.
Follow-up question: Here is the full set--I was able to further reduce to "feature.id". Can these lines be combined?
{% set gridEnabled = grid.enabled %}
{% set gridEnabled = gridEnabled|merge({(feature.id): true}) %}
{% set grid = grid|merge({'enabled':gridEnabled}) %}
Very easy actually,
{% set gridEnabled = gridEnabled|merge({(featureId): true}) %}
(edit) follow-up
{% set grid = grid | merge({'enabled' : (grid.enabled | merge({(featureId):1,}))}) %}

TWIG, dynamic associative array key

I need to create a array with a dynamic key.
Example:
{% set key = 'a' %}
{% set value = 'b' %}
{% set array = {key:value} %}
I find a workaround for this problem: if you surround the key with parenthesis the key of array take the value of the variable instead of the name. So try simple this:
{% set array = { (key): value} %}
Here a working solutions

how to use split twig template field content type in drupal 8?

I need your help to solve the problem with split in twig.
I want to separate my var {{label}} in an array,
try using
{% set array = label | split (" ")%}
returns empty
I want to separate a field node in an array,
try using
{% set array = content.field_fieldname | split (" ")%}
returns empty
if someone helps me identify what I'm doing wrong, or I indicates a guide and / or tutorial that can solve my problem. Thanks
How do you check that the array is empty?
{% set label = 'this is a label'%}
{% set array = label | split (" ")%}
{% for elem in array %}
{{loop.index}} - {{ elem}}
{% endfor %}
Check this code online here
{{ label }} is in the template as an object, I solved get the string
{% set title = items[0]['content']['#context']["value"]|split(' ', 2) %}
<h1>{{ title[0] }}</h1>
{{ title[1] }}

Using variables inside Twig functions

I just can't get this to work:
It should set var_2 based on the URL query string value of var_1
The problem is where I call var_1 with {{var_1}}
I've tried various other methods but all throw different errors.
// var_3 set elsewhere
{% set var_1 %}test-{{var_3}}{% endset %}
{% set var_2 = app.request.get({{var_1}}) %}
// need var_2 set for rest of script
You can't use another tag ({{ ... }}) inside a twig tag ({% ... %}). So this is not going to work:
{% set var_2 = app.request.get({{var_1}}) %}
A solution is to just put the variable in function argument:
{% set var_2 = app.request.get(var_1) %}
You don't need to (and often cannot) use {{ }} within twig logic. {{ }} is used to output something to the response. To use a variable in line just name the variable. Also remember that ~ will join strings, but some people don't like using it for some reason!
{% set var_1 = 'test-' ~ var_3 %}
{% set var_2 = app.request.get(var_1) %}

Twig - Append string data to same variable

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.

Resources