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
104
votes
4 answers

How to convert string to uppercase / lowercase in Jinja2?

I am trying to convert to upper case a string in a Jinja template I am working on. In the template documentation, I read: upper(s) Convert a value to uppercase. So I wrote this code: {% if student.department == "Academy" %} Academy {% elif …
Xar
  • 7,572
  • 19
  • 56
  • 80
98
votes
7 answers

How to pass a list from Python, by Jinja2 to JavaScript

Let's say I have a Python variable: list_of_items = ['1','2','3','4','5'] and I pass it to Jinja by rendering HTML, and I also have a function in JavaScript called somefunction(variable). I am trying to pass each item of list_of_items. I tried…
user1843766
  • 1,013
  • 1
  • 8
  • 5
94
votes
2 answers

Get loop index of outer loop

In jinja, the variable loop.index holds the iteration number of the current running loop. When I have nested loops, how can I get in the inner loop the current iteration of an outer loop?
flybywire
  • 261,858
  • 191
  • 397
  • 503
89
votes
2 answers

how to iterate over a list of list in jinja

I have a list of list like : [[elem0, elem1, elem2], [elem3, elem4, elem5], [elem6, elem7, elem8], ...] I wrote the follow template file : {% for result in results %} result[0] …
stamaimer
  • 6,227
  • 5
  • 34
  • 55
89
votes
4 answers

Check if key exists in a Python dict in Jinja2 templates

I have a python dictionary: settings = { "foo" : "baz", "hello" : "world" } This variable settings is then available in the Jinja2 template. I want to check if a key myProperty exists in the settings dict within my template, and if so take…
Amal Antony
  • 6,477
  • 14
  • 53
  • 76
89
votes
1 answer

Jinja2 template not rendering if-elif-else statement properly

I am trying to set the text color using css in a jinja2 template. In the following code I want to set the output string to print in a specific font color if the variable contains a string. Everytime the template is generated though it prints in…
Matty
  • 1,100
  • 1
  • 9
  • 13
84
votes
2 answers

'if' statement in jinja2 template

I'm trying to write an if statement in jinja template: {% for key in data %} {% if key is 'priority' %}

('Priority: ' + str(data[key])

{% endif %} {% endfor %} the statement I'm trying to translate in Python is: if key ==…
Luisito
  • 895
  • 1
  • 7
  • 9
84
votes
5 answers

Does PyCharm support Jinja2?

A bottle project of mine uses Jinja2. PyCharm does not automatically recognize it and shows such lines as errors. Is there a way to make Jinja2 work?
k0pernikus
  • 60,309
  • 67
  • 216
  • 347
83
votes
4 answers

How to use float filter to show just two digits after decimal point?

I am using Flask/Jinja2 template to show a number using |float filter. Here is my code {% set proc_err = nb_err|length / sum * 100 %} ({{proc_err|float}}%) Output is a bit awkward: 17/189 (8.99470899471%) I am looking for a way to make the places…
Blaise
  • 7,230
  • 6
  • 43
  • 53
82
votes
6 answers

jinja2 how to remove trailing newline

I'm using jinja 2 to output a yaml file but can't seem to get rid of a trailing newline and the end of a for loop. Eg the below - request: path: {{ path }} headers: origin: 'somedomain.com' user-agent: 'agent' …
Yunti
  • 6,761
  • 12
  • 61
  • 106
80
votes
5 answers

How can I test jinja2 templates in ansible?

Sometimes I need to test some jinja2 templates that I use in my ansible roles. What is the simplest way for doing this? For example, I have a template (test.j2): {% if users is defined and users %} {% for user in users %}{{ user }} {% endfor %} {%…
Alex
  • 5,728
  • 5
  • 20
  • 20
77
votes
2 answers

What dashes mean in jinja templates?

Found some of these in jinja files: {%- else -%} And also {% if freeswitch_dispatcher -%} See the dashes ? Any idea what it's for ?
Anto
  • 6,806
  • 8
  • 43
  • 65
76
votes
1 answer

How to make a for loop in Jinja?

I want to make a for-loop that goes from 0 to 10 in Jinja. How do I do it?
promo
  • 789
  • 1
  • 5
  • 4
76
votes
1 answer

How to write a multiline Jinja statement

I have an if statement in my Jinja templates which I want to write it in multines for readability reasons. Consider the case {% if (foo == 'foo' or bar == 'bar') and (fooo == 'fooo' or baar == 'baar') etc.. %}
topless
  • 8,069
  • 11
  • 57
  • 86
72
votes
8 answers

Can a Jinja variable's scope extend beyond in an inner block?

I have the following Jinja template: {% set mybool = False %} {% for thing in things %}
    {% if current_user %} {% if current_user.username == thing['created_by']['username'] %} …
Matt Norris
  • 8,596
  • 14
  • 59
  • 90