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
59
votes
3 answers

How to get the first element of a list from the output of setup module in Ansible?

I received the following data from the setup module: "ansible_nodename": "3d734bc2a391", "ansible_os_family": "RedHat", "ansible_pkg_mgr": "yum", "ansible_processor": [ "AuthenticAMD", "AMD PRO A10-8700B R6, 10 Compute Cores…
thinkingmonster
  • 5,063
  • 8
  • 35
  • 57
59
votes
3 answers

How can I break a for loop in jinja2?

How can I break out of a for loop in jinja2? my code is like this: {% for page in pages if page.tags['foo'] == bar %} {{page.title}} {% break %} {% endfor %} I have more than one page that has this condition and I want to end the…
Taxellool
  • 4,063
  • 4
  • 21
  • 38
59
votes
3 answers

Jinja2 correctly indent included block

I have two files: base.html {{title}} {% block content %} {% endblock %} register.html {% extends…
Fenikso
  • 9,251
  • 5
  • 44
  • 72
59
votes
5 answers

Using sass with Flask and jinja2

I would like to include a sass compiler in my Flask application. Is there a generally accepted way of doing this?
kasperhj
  • 10,052
  • 21
  • 63
  • 106
58
votes
4 answers

Django: Switching to Jinja2?

I've got a couple small (500 or 600 lines of template code) Django sites, and I'd like to migrate them to using Jinja2… But I'd like to get some idea of how much work it will be. So, in general, about how much work is it to migrate a small Django…
David Wolever
  • 148,955
  • 89
  • 346
  • 502
58
votes
7 answers

Unable to pass jinja2 variables into javascript snippet

How do i pass jinja2 data into javascript. I have a Flask REST url as /logs/ I am trying use .getJSON() to query the above URL and hence would want to pass the jinja2 data which has the testcasename to .getJSON function. sample…
deeshank
  • 4,286
  • 4
  • 26
  • 32
58
votes
2 answers

jinja2.exceptions.TemplateNotFound error

i use flask and i got this error when i call this url: /login Here's my login method: @app.route('/login') def login(): if authenticateForPanel(): return redirect(url_for("panel")) else: getParam =…
saidozcan
  • 2,115
  • 9
  • 27
  • 39
56
votes
2 answers

Is it possible to use AngularJS with the Jinja2 template engine?

I have a Flask site and I want to use the AngularJS JavaScript framework. Unfortunately, it seems as if the delimiters overlap. How do I use Angular with Jinja2 if both rely on double curly braces ({{ expr }})? Is it even possible?
user559633
56
votes
1 answer

Flask + Jinja: Pass Data to a Base Template/All Templates

I have a method that returns data which is needed in my base template (content for a global footer). How do either (1) pass a variable into the base template (which other templates extend) or (2) pass a variable to all templates globally without…
okoboko
  • 4,332
  • 8
  • 40
  • 67
56
votes
2 answers

What do the chars %7D mean in an url query?

If I access my webapp with the url /vi/5907399890173952.html then it works but when I look in the log files then googlebot is trying to access a similar url which generates an exception: /vi/5907399890173952.html%7D%7D what does it mean and how can…
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
56
votes
3 answers

Pass variables to Flask's render_template

I want to pass multiple variables from my Flask view to my Jinja template. Right now, I can only pass one. How do I pass multiple variable when rendering a template? @app.route("/user//post/", methods=["GET", "POST"]) def…
Afeez Aziz
  • 1,337
  • 2
  • 12
  • 26
55
votes
4 answers

zip(list1, list2) in Jinja2?

I'm doing code generation in Jinja2 and I frequently want to iterate through two lists together (i.e. variables names and types), is there a simple way to do this or do I need to just pass a pre-zipped list? I was unable to find such a function in…
John Salvatier
  • 3,077
  • 4
  • 26
  • 31
55
votes
5 answers

What is the fastest template system for Python?

Jinja2 and Mako are both apparently pretty fast. How do these compare to (the less featured but probably good enough for what I'm doing) string.Template ?
Josh Gibson
  • 21,808
  • 28
  • 67
  • 63
55
votes
6 answers

Jinja Templates - Format a float as comma-separated currency

I'm trying to format a float as comma-separated currency. E.g. 543921.9354 becomes $543,921.94. I'm using the format filter in Jinja templates, which seems to mimic the % operator in Python rather than the Python format function? How can I…
Kyle Johnson
  • 1,605
  • 2
  • 17
  • 26
53
votes
6 answers

How to get list of all variables in jinja 2 templates

I am trying to get list of all variables and blocks in a template. I don't want to create my own parser to find variables. I tried using following snippet. from jinja2 import Environment, PackageLoader env = Environment(loader=PackageLoader('gummi',…
Kracekumar
  • 19,457
  • 10
  • 47
  • 56