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
455
votes
5 answers

Get lengths of a list in a jinja2 template

How do I get the number of elements in a list in jinja2 template? For example, in Python: print(template.render(products=[???])) and in jinja2 You have {{what goes here?}} products
flybywire
  • 261,858
  • 191
  • 397
  • 503
345
votes
4 answers

Set variable in jinja

I would like to know how can I set a variable with another variable in jinja. I will explain, I have got a submenu and I would like show which link is active. I tried this: {% set active_link = {{recordtype}} -%} where recordtype is a variable…
MyTux
  • 3,459
  • 2
  • 15
  • 4
283
votes
9 answers

In Jinja2, how do you test if a variable is undefined?

Converting from Django, I'm used to doing something like this: {% if not var1 %} {% endif %} and having it work if I didn't put var1 into the context. Jinja2 gives me an undefined error. Is there an easy way to say {% if var1 == None %} or similar?
freyley
  • 4,145
  • 3
  • 20
  • 25
272
votes
5 answers

How to output loop.counter in python jinja template?

I want to be able to output the current loop iteration to my template. According to the docs, there is a loop.counter variable that I am trying to use:
    {% for user in userlist %}
  • {{ user }} {{loop.counter}}
  • {% if…
Rolando
  • 58,640
  • 98
  • 266
  • 407
271
votes
2 answers

Jinja2 shorthand conditional

Say I have this: {% if files %} Update {% else %} Continue {% endif %} In PHP, say, I can write a shorthand conditional, like: Is there then a way I can translate this to work in a jinja2 template: 'yes'…
Ahmed Nuaman
  • 12,662
  • 15
  • 55
  • 87
266
votes
7 answers

Passing HTML to template using Flask/Jinja2

I'm building an admin for Flask and SQLAlchemy, and I want to pass the HTML for the different inputs to my view using render_template. The templating framework seems to escape the HTML automatically, so all <"'> characters are converted to HTML…
sharvey
  • 7,635
  • 7
  • 48
  • 66
266
votes
3 answers

How to output a comma delimited list in jinja python template?

If I have a list of users say ["Sam", "Bob", "Joe"], I want to do something where I can output in my jinja template file: {% for user in userlist %} {{ user }} {% if !loop.last %} , {% endif…
Rolando
  • 58,640
  • 98
  • 266
  • 407
249
votes
10 answers

How do I format a date in Jinja2?

Using Jinja2, how do I format a date field? I know in Python I can simply do this: print(car.date_of_manufacture.strftime('%Y-%m-%d')) But how do I format the date in Jinja2?
Ambrosio
  • 3,789
  • 6
  • 22
  • 13
237
votes
13 answers

Jinja2 template variable if None Object set a default value

How to make a variable in jijna2 default to "" if object is None instead of doing something like this? {% if p %} {{ p.User['first_name']}} {% else %} NONE {%endif %} So if object p is None I want to default the…
mcd
  • 6,446
  • 9
  • 27
  • 32
203
votes
15 answers

Call a python function from jinja2

I am using jinja2, and I want to call a python function as a helper, using a similar syntax as if I were calling a macro. jinja2 seems intent on preventing me from making a function call, and insists I repeat myself by copying the function into a…
Lee
  • 2,539
  • 3
  • 19
  • 8
178
votes
3 answers

Convert integer to string Jinja

I have an integer {% set curYear = 2013 %} In {% if %} statement I have to compare it with some string. I can't set curYear to string at the beginning because I have to decrement it in loop. How can I convert it?
Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79
166
votes
5 answers

How to load jinja template directly from filesystem

The jinja API document at pocoo.org states: The simplest way to configure Jinja2 to load templates for your application looks roughly like this: from jinja2 import Environment, PackageLoader env = Environment(loader=PackageLoader('yourapplication',…
Juan Tomas
  • 4,905
  • 3
  • 14
  • 19
157
votes
9 answers

How can I pass data from Flask to JavaScript in a template?

My app makes a call to an API that returns a dictionary. I want to pass information from this dict to JavaScript in the view. I am using the Google Maps API in the JS, specifically, so I'd like to pass it a list of tuples with the long/lat…
mea
  • 1,573
  • 2
  • 10
  • 5
140
votes
13 answers

Reload Flask app when template file changes

By default, when running Flask application using the built-in server (Flask.run), it monitors its Python files and automatically reloads the app if its code changes: * Detected change in '/home/xion/hello-world/app.py', reloading * Restarting with…
Xion
  • 22,400
  • 10
  • 55
  • 79
134
votes
5 answers

How to iterate through a list of dictionaries in Jinja template?

I tried: list1 = [{"username": "abhi", "pass": 2087}] return render_template("file_output.html", list1=list1) In the template: {% for dictionary in list1…
user3089927
  • 3,575
  • 8
  • 25
  • 33
1
2 3
99 100
Key Value