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
53
votes
1 answer

Flask Dynamic data update without reload page

i'm trying to create something like Google Suggest Tool (via suggest api http://suggestqueries.google.com/complete/search?output=toolbar&hl=ru&q=query ) I'm listening input changes, and send data go GET:…
Konstantin Rusanov
  • 6,414
  • 11
  • 42
  • 55
53
votes
5 answers

Is there a direct approach to format numbers in jinja2?

I need to format decimal numbers in jinja2. When I need to format dates, I call the strftime() method in my template, like this: {{ somedate.strftime('%Y-%m-%d') }} I wonder if there is a similar approach to do this over numbers. Thanks in…
Lucas
  • 1,239
  • 4
  • 15
  • 26
53
votes
2 answers

What does this "-" in jinja2 template engine do?

I am learning jinja2 because Google App Engine recommends it. I found this example on Wikipedia: http://en.wikipedia.org/wiki/Jinja_%28template_engine%29 {%- for item in item_list %} {{ item }}{% if not loop.last %},{% endif %} {%- endfor…
Gaby Solis
  • 2,427
  • 5
  • 21
  • 27
52
votes
12 answers

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

When I recently deployed my project that includes Flask==1.0.2 and Jinja2>=2.10.1, I got the following error. It was running fine when I deployed it the previous day. I tried updating Jinja2 but that didn't fix the issue. File…
young_minds1
  • 1,181
  • 3
  • 10
  • 25
52
votes
6 answers

use a css stylesheet on a jinja2 template

I am making a website using html, css, flask and jinja2. I have a page working on a flask server, the buttons and labels etc. are displayed, but the css stylesheet I have is not loaded in. How would I link a stylesheet to a jinja2 template. I have…
ptolemy0
  • 799
  • 1
  • 5
  • 13
51
votes
4 answers

What is the process to create pdf reports with charts from a DB?

I have a database generated by a survey to evaluate university professors. What I want is a python script that takes the information from that database, generates a graphing table for each user, creates graphs for each user, and then renders it in a…
ozo
  • 883
  • 1
  • 10
  • 18
51
votes
2 answers

Mako or Jinja2?

I didn't find a good comparison of jinja2 and Mako. What would you use for what tasks ? I personnaly was satisfied by mako (in a pylons web app context) but am curious to know if jinja2 has some nice features/improvements that mako doesn't ? -or…
ychaouche
  • 4,922
  • 2
  • 44
  • 52
51
votes
2 answers

Multiple blocks of same name in Jinja2

In Jinja2, I have a base template like this: {% block title %}{% endblock %} - example.com [...]

{% block title %}{% endblock %} - example.com

Jinja2, then, fails with the following message: lines = [self.message, ' …
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
49
votes
5 answers

Getting a request parameter in Jinja2

How can I retrieve a request param a in Jinja2 template? http://foo.bar?a=1
Shankar Cabus
  • 9,302
  • 7
  • 33
  • 43
49
votes
3 answers

jinja2 load template from string: TypeError: no loader for this environment specified

I'm using Jinja2 in Flask. I want to render a template from a string. I tried the following 2 methods: rtemplate = jinja2.Environment().from_string(myString) data = rtemplate.render(**data) and rtemplate = jinja2.Template(myString) data =…
user3605780
  • 6,542
  • 13
  • 42
  • 67
49
votes
9 answers

Changing the active class of a link with the twitter bootstrap css in python/flask

I got the following html snippet from my page template.html.
47
votes
1 answer

How to build up a HTML table with a simple for loop in Jinja2?

I'm just learning Jinja2. I have never done any templating before so I find the documentation very confusing right now. How do I do build up a HTML table with a simple FOR loop? My template looks something like this: {% for item in items %} …
NomadAlien
  • 9,090
  • 6
  • 25
  • 22
45
votes
9 answers

How to render by Vue instead of Jinja

This above is my…
ramsay
  • 3,197
  • 2
  • 14
  • 13
45
votes
3 answers

raise an exception in jinja if we passed in a variable that is not present in the template

Is there a method for jinja2 to raise an exception when we pass a variable that is not present in the template? PS: This is different(or opposite) from raising an exception when a variable is present in the template but it is not passed. For this I…
rakesh
  • 451
  • 1
  • 4
  • 3
45
votes
5 answers

How do I get an Ansible template to honor new lines after a conditional

The template looks like this: solr.replication.master= {% if ansible_eth0.ipv4.address == servermaster.eth0 %} false {% else %} true {% endif %} solr.replication.slave=false And the output should look like…
Oliver Lorton
  • 709
  • 1
  • 6
  • 21