Questions tagged [jinja2]

Jinja2 is a fast template engine for Python. It has full Unicode support, auto-escaping, inheritance, macros, and many other features.

Jinja2 is a fast template engine for Python. It is the default template engine in , can be integrated in other frameworks such as , or can be used without a framework. Features include:

  • Compose complex pages by inheriting from or including other templates
  • Automatic HTML escaping to prevent cross site scripting
  • Write reusable template macros and import them in other templates
  • Call functions with positional and keyword arguments
  • High performance with just in time compilation to Python bytecode
  • Optional ahead of time compilation
  • Optional sandboxed execution environment
  • Wide range of helpful functions for template designers
9474 questions
130
votes
2 answers

Link to Flask static files with url_for

How do you use url_for in Flask to reference a file in a folder? For example, I have some static files in the static folder, some of which may be in subfolders such as static/bootstrap. When I try to serve a file from static/bootstrap, I get an…
user1431282
  • 6,535
  • 13
  • 51
  • 68
122
votes
3 answers

Split string into list in jinja?

I have some variables in a jinja2 template which are strings seperated by a ';'. I need to use these strings separately in the code. i.e. the variable is variable1 = "green;blue" {% list1 = {{ variable1 }}.split(';') %} The grass is {{ list1[0] }}…
user3605780
  • 6,542
  • 13
  • 42
  • 67
119
votes
8 answers

How to increment a variable on a for loop in jinja template?

I would like to do something like: variable p is from test.py which is a list ['a','b','c','d'] {% for i in p %} {{variable++}} {{variable}} result output is: 1 2 3 4
user422100
  • 2,099
  • 6
  • 22
  • 22
118
votes
3 answers

How do I include a HTML file in a Jinja2 template?

I am using Flask micro-framework for my server which uses Jinja templates. I have a parent template.html and some children templates called child1.html and child2.html, some of these children templates are pretty large HTML files and I would like to…
quapka
  • 2,799
  • 4
  • 21
  • 35
118
votes
3 answers

Jinja2 inline comments

How can I put comments inside Jinja2 argument list declaration ? Everything I have tried gives an error: jinja2.exceptions.TemplateSyntaxError: unexpected char u'#' {{ Switch('var', [('1', 'foo'), # comment 1 ('2', 'bar'), ## comment…
kimstik
  • 1,611
  • 2
  • 15
  • 14
117
votes
1 answer

Escape jinja2 syntax in a jinja2 template

I serve dynamic pages from Jinja2 templates in Flask. Now I am defining client-side templates in say, Jinja2-clone Nunjucks inside a script tag. Problem is, the client-side templates has syntax like <% %> that Flask's Jinja2 interpreter may…
Jesvin Jose
  • 22,498
  • 32
  • 109
  • 202
117
votes
2 answers

How do you sort a list in Jinja2?

I am trying to do this: {% for movie in movie_list | sort(movie.rating) %} But that's not right...the documentation is vague...how do you do this in Jinja2?
Nick Perkins
  • 8,034
  • 7
  • 40
  • 40
116
votes
8 answers

ImportError: cannot import name 'escape' from 'jinja2'

I am getting the error ImportError: cannot import name 'escape' from 'jinja2' When trying to run code using the following…
Bheshaj
  • 1,262
  • 2
  • 3
  • 6
114
votes
8 answers

Is there an idiomatic file extension for Jinja templates?

I need to programatically distinguish between Jinja template files, other template files (such as ERB), and template-less plain text files. According to Jinja documentation: A Jinja template doesn’t need to have a specific extension: .html, .xml,…
Chris Tonkinson
  • 13,823
  • 14
  • 58
  • 90
113
votes
10 answers

Escaping double curly braces in Ansible

How to escape double curly braces in Ansible 1.9.2? For instance, how can I escape double curly braces in the following shell command? - name: Test shell: "docker inspect --format '{{ .NetworkSettings.IPAddress }}' instance1"
Davide Guerri
  • 1,887
  • 2
  • 17
  • 25
111
votes
3 answers

Change the value of a variable inside a loop

I want to change the value of the variable declared outside the loop within a loop. But, even when changing hit inside the loop, it keeps the initial value outside the loop. {% set foo = False %} {% for item in items %} {% set foo = True %} {%…
Shankar Cabus
  • 9,302
  • 7
  • 33
  • 43
110
votes
3 answers

Ansible: filter a list by its attributes

I have variable named "network" registered in Ansible: { "addresses": { "private_ext": [ { "type": "fixed", "addr": "172.16.2.100" } ], …
Guido
  • 46,642
  • 28
  • 120
  • 174
109
votes
6 answers

String concatenation in Jinja

I just want to loop through an existing list and make a comma delimited string out of it. Something like this: my_string = 'stuff, stuff, stuff, stuff' I already know about loop.last, I just need to know how to make the third line in my code below…
KacieHouser
  • 1,885
  • 4
  • 22
  • 35
107
votes
5 answers

How to pass selected, named arguments to Jinja2's include context?

Using Django templating engine I can include another partial template while setting a custom context using named arguments, like this: {% include "list.html" with articles=articles_list1 only %} {% include "list.html" with articles=articles_list2…
NiKo
  • 11,215
  • 6
  • 46
  • 56
107
votes
3 answers

How do I render jinja2 output to a file in Python instead of a Browser

I have a jinja2 template (.html file) that I want to render (replace the tokens with values from my py file). Instead of sending the rendered result to a browser, however, I want to write it to a new .html file. I would imagine the solution would…
Bill G.
  • 1,417
  • 2
  • 12
  • 18