Save Jinja Filter Result

The basics of what I'm trying to do is use a "random" filter to select a random item from my list, but then I want to use this randomly selected item in several places.

How to set a filter result for a variable that I can use in several places.

If I call the "random" filter several times, there is little chance that they will be the same.

Essentially what I want to do:

{% set image = {{ images | random }} %} 

obviously this does not work.

+7
source share
3 answers

Use a delimited filter {{ }} .

 {% set image = images|random %} 

Jinja stores globals and filters in two different namespaces (dictionaries), which prevents their interchangeability.

+10
source

| in Jinja just passes a variable to a function. Just call the function and it should work:

 {% set image = random(images) %} 
+3
source

This does not work in a loop, I used this code:

 {% set result = result | replace('x','y') %} 
0
source

All Articles