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 developed and supported by the Pocoo Team. 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

References

7537 questions
372
votes
3 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
  • 232,954
  • 184
  • 384
  • 491
281
votes
3 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
  • 2,819
  • 2
  • 13
  • 4
219
votes
6 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
  • 3,385
  • 3
  • 17
  • 24
211
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
  • 11,324
  • 13
  • 50
  • 80
208
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
  • 44,077
  • 84
  • 230
  • 353
207
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,149
  • 5
  • 19
  • 13
207
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: http://wsgiarea.pocoo.org/jinja/docs/loops.html, there is a loop.counter variable that I am trying to use. I have the following:
    {% for user in…
Rolando
  • 44,077
  • 84
  • 230
  • 353
198
votes
6 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
  • 6,076
  • 7
  • 44
  • 66
171
votes
10 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
  • 4,766
  • 7
  • 23
  • 31
168
votes
13 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,189
  • 3
  • 17
  • 8
141
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,413
  • 2
  • 10
  • 5
140
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?
Glen Swift
  • 10,962
  • 14
  • 43
  • 73
107
votes
4 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
  • 3,505
  • 3
  • 10
  • 18
105
votes
11 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
  • 20,785
  • 9
  • 48
  • 77
100
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
  • 5,654
  • 12
  • 44
  • 64
1
2 3
99 100