Get loop index of outer loop
Store it in a variable, for example: {% for i in a %} {% set outer_loop = loop %} {% for j in a %} {{ outer_loop.index }} {% endfor %} {% endfor %}
Store it in a variable, for example: {% for i in a %} {% set outer_loop = loop %} {% for j in a %} {{ outer_loop.index }} {% endfor %} {% endfor %}
Jinja is a dependency of Flask and Flask V1.X.X uses the escape module from Jinja, however recently support for the escape module was dropped in newer versions of Jinja. To fix this issue, simply update to the newer version of Flask V2.X.X in your requirements.txt where Flask no longer uses the escape module from Jinja. … Read more
To pass some context data to javascript code, you have to serialize it in a way it will be “understood” by javascript (namely JSON). You also need to mark it as safe using the safe Jinja filter, to prevent your data from being htmlescaped. You can achieve this by doing something like that: The view … Read more
Filters are used with the |filter syntax: {% elif student.department|upper != “MATHS DEPARTMENT” %} Maths department {% endif %} or you can use the str.upper() method: {% elif student.department.upper() != “MATHS DEPARTMENT” %} Maths department {% endif %} Jinja syntax is Python-like, not actual Python.
If stuffs is a list of strings, just this would work: {{ stuffs|join(“, “) }} See join filter documentation, as well as filters in general documentation. p.s. More reader friendly way {{ my ~ ‘, ‘ ~ string }}
Jinja2 has an extension that enables the with keyword – it won’t give you the same syntax as Django, and it may not work the way you anticipate but you could do this: {% with articles=articles_list1 %} {% include “list.html” %} {% endwith %} {% with articles=articles_list2 %} {% include “list.html” %} {% endwith %} … Read more
2021 update:: Jinja now officially recommends using the extension .jinja. check docs Update: Things changed since I wrote this answer, .jinja2 and .j2 are trending. Jinja Authors did not define a default extension. Most of Jinja template editors like Vim extension, TextMate extension, Emacs extension, and PyCharm mention no default extension to enforce Jinja highlighting. … Read more
Try also dictionary-based approach. It seems to be less ugly. {% set vars = {‘foo’: False} %} {% for item in items %} {% if vars.update({‘foo’: True}) %} {% endif %} {% if vars.foo %} Ok(1)! {% endif %} {% endfor %} {% if vars.foo %} Ok(2)! {% endif %} This also renders: Ok(1)! Ok(2)!
How about something like this? from jinja2 import Environment, FileSystemLoader env = Environment(loader=FileSystemLoader(‘templates’)) template = env.get_template(‘test.html’) output_from_parsed_template = template.render(foo=’Hello World!’) print(output_from_parsed_template) # to save the results with open(“my_new_file.html”, “w”) as fh: fh.write(output_from_parsed_template) test.html <h1>{{ foo }}</h1> output <h1>Hello World!</h1> If you are using a framework, such as Flask, then you could do this at the … Read more
To filter a list of dicts you can use the selectattr filter together with the equalto test: network.addresses.private_man | selectattr(“type”, “equalto”, “fixed”) The above requires Jinja2 v2.8 or later (regardless of Ansible version). Ansible also has the tests match and search, which take regular expressions: match will require a complete match in the string, while … Read more