Reference Template Variable Within Jinja Expression

Reference template variable within Jinja expression

Everything inside the {{ ... }} is a Python-like expression. You don't need to use another {{ ... }} inside that to reference variables.

Drop the extra brackets:

<h1>you uploaded {{ name }}<h1>
<a href="{{ url_for('moremagic', filename=name) }}">Click to see magic happen</a>

(Note that the url_for() function takes the endpoint name, not a URL path; the name defaults to the name of the function, moremagic in your example).

Reference variables in expressions in Ansible/Jinja

Yes. It's possible.

"{{ myVariable.someItem[someVariableItem].someOtherItem }}"

For example the play below

- hosts: localhost
vars:
my_variable_item: item_z
my_dict:
item_x:
other_item_a: 1
item_y:
other_item_b: 2
item_z:
other_item_c: 3
tasks:
- debug:
msg: '{{ my_dict[my_variable_item].other_item_c }}'

gives

"msg": "3"

Variable inside variable in Jinja2

You are looking for Jinja macros; functions that produce output when called:

{% macro she_greatest(name) -%}
Goddess {{name}} greatest of all time!
{%- endmacro %}

{% set first_list = ['Kira', 'Kristina'] %}
Any text
{% for name in first_list %}
{{ she_greatest(name) }}
{% endfor %}
{% set second_list = ['Sasha', 'Katya'] %}
Another text
{% for name in second_list %}
{{ she_greatest(name) }}
{% endfor %}

Demo:

>>> from jinja2 import Template
>>> demo = '''
... {% macro she_greatest(name) -%}
... Goddess {{name}} greatest of all time!
... {%- endmacro %}
...
... {% set first_list = ['Kira', 'Kristina'] %}
... Any text
... {% for name in first_list %}
... {{ she_greatest(name) }}
... {% endfor %}
... {% set second_list = ['Sasha', 'Katya'] %}
... Another text
... {% for name in second_list %}
... {{ she_greatest(name) }}
... {% endfor %}
... '''
>>> print(Template(demo).render())




Any text

Goddess Kira greatest of all time!

Goddess Kristina greatest of all time!


Another text

Goddess Sasha greatest of all time!

Goddess Katya greatest of all time!

or, applying this to your real-life example:

{% macro kira_function_link(url, color, sitename) -%}
<a onclick="KiraFunction(document.all.sitename.value, '{{url}}');" class="{{color}}">{{sitename}}</a>
{%- endmacro %}

{% set first_list = [
('https://Alpha.com', 'Foo', 'Alpha'),
('https://Bravo.ru', 'Bar', 'Beta')
] %}
<div class="KiraCategory">First</div>
{% for url, color, sitename in first_list %}
{{ kira_function_link(url, color, sitename) }}
{% endfor %}
{% set second_list = [
('https://Charlie.net', 'Baz', 'Gamma'),
] %}
<div class="KiraCategory">Second</div>
{% for url, color, sitename in second_list %}
{{ kira_function_link(url, color, sitename) }}
{% endfor %}

How do I assign a jinja2 variable value to use later in template?

Use {% set %}:

{% if 'clear' in forcast_list[4] %}
{% set img = "sunny.png" %}
{% elif "cloudy" in forcast_list[4] %}
{% set img = "sun-cloudy-thunder.png" %}
{% endif %}

More information about assignments in jinja2 here.

Or simply do the conditionals within python and pass the result to jinja2 template:

if 'clear' in forcast_list[4]:
img = "sunny.png"
elif 'cloudy' in forcast_list[4]:
img = "sun-cloudy-thunder.png"
...
return render_template('foo.html', img=img)

use variables inside image src flask

Definitely a string formatting error

Do this instead

<img src="{{ url_for('static',filename='output/{}.png'.format(file_name) ) }}">


Related Topics



Leave a reply



Submit