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
72
votes
6 answers

Import a Python module into a Jinja template?

Is it possible to import a Python module into a Jinja template so I can use its functions? For example, I have a format.py file that contains methods for formatting dates and times. In a Jinja macro, can I do something like the following? {% from…
Matt Norris
  • 8,596
  • 14
  • 59
  • 90
71
votes
10 answers

Check if an array is not empty in Jinja2

I need to check if the variable texts is defined or not in index.html. If the variable is defined and not empty then I should render the loop. Otherwise, I want to show the error message {{error}}. Basically this in PHP if (isset($texts) &&…
daniel__
  • 11,633
  • 15
  • 64
  • 91
70
votes
1 answer

Length of string in Jinja/Flask

Jinja unfortunately does not support executing arbitrary Python code, such as {% if len(some_var)>1 %} ... {% endif %} My current workaround is to use the deprecated, ugly, double-underscore method: {% if some_var.__len__()>1 %} ... {% endif…
wuxiekeji
  • 1,772
  • 2
  • 15
  • 22
69
votes
6 answers

Flask: current page in request variable

In a template, how do I get what page I'm currently on? I'd rather not pass a variable like page , especially when I know some request.xxx can provide me with the information.
  • atp
    • 30,132
    • 47
    • 125
    • 187
    69
    votes
    1 answer

    Flask - Accessing the config variable in the template

    I am using Flask version 0.7. I have stored the path of static content in a configuration file and loaded it using app.config.from_envvar() Can I be able to access this config variable in the template without passing the variables…
    ranendra
    • 2,492
    • 2
    • 19
    • 29
    69
    votes
    2 answers

    *args, **kwargs in jinja2 macros

    How are extra args & kwargs handled for a Jinja2 macro? The documentation isn't exactly clear offhand. For example, this is clearly wrong: {% macro example_1(one, two, **kwargs) %} do macro stuff {% endmacro %} which results…
    blueblank
    • 4,724
    • 9
    • 48
    • 73
    68
    votes
    10 answers

    Ansible: Get all the IP addresses of a group

    Let's imagine an inventory file like this: node-01 ansible_ssh_host=192.168.100.101 node-02 ansible_ssh_host=192.168.100.102 node-03 ansible_ssh_host=192.168.100.103 node-04 ansible_ssh_host=192.168.100.104 node-05…
    Forivin
    • 14,780
    • 27
    • 106
    • 199
    68
    votes
    5 answers

    Suppress "None" output as string in Jinja2

    How do I persuade Jinja2 to not print "None" when the value is None? I have a number of entries in a dictionary and I would like to output everything in a single loop instead of having special cases for different keywords. If I have a value of None…
    Spaceghost
    • 6,835
    • 3
    • 28
    • 42
    67
    votes
    2 answers

    Remove unnecessary whitespace from Jinja rendered template

    I'm using curl to watch the output of my web app. When Flask and Jinja render templates, there's a lot of unnecessary white space in the output. It seems to be added by rendering various components from Flask-WTF and Flask-Bootstrap. I could strip…
    Hexatonic
    • 2,251
    • 2
    • 21
    • 26
    66
    votes
    1 answer

    jinja2 - how to put a block in an if statement?

    I am trying to use an if to determine which block should fill my {% block content %}. I have a base.html which has a default {% block content %} and this template is extending base.html. So I tried this: {% extends "base.html" %} {% if condition ==…
    Taxellool
    • 4,063
    • 4
    • 21
    • 38
    65
    votes
    1 answer

    Flask Template Not found

    Implementing a simple static site from flask, but the browser says template not found, the shell returned 404 jinja2.exceptions.TemplateNotFound TemplateNotFound: template.html The main python code: from flask import Flask, render_template app =…
    saviour123
    • 1,095
    • 2
    • 13
    • 21
    64
    votes
    2 answers

    range in jinja2 inside a for loop

    I have a nested list. I need to iterate through a list and keep it in for loop as shown below. {% for alpha in list %}
    Chandan Gupta
    • 1,410
    • 2
    • 13
    • 29
    64
    votes
    3 answers

    How to use g.user global in flask

    As I understand the g variable in Flask, it should provide me with a global place to stash data like holding the current user after login. Is this correct? I would like my navigation to display my user's name, once logged in, across the site. My…
    Mittenchops
    • 18,633
    • 33
    • 128
    • 246
    62
    votes
    3 answers

    How to dynamically select template directory to be used in flask?

    By default flask uses template files stored in "template" directory : /flaskapp /application.py /templates /hello.html Is there any way to dynamically choose template directory according to user logged in? This is how I want the…
    anujkk
    • 1,333
    • 3
    • 14
    • 22
    61
    votes
    3 answers

    Is it possible to perform Includes with flask?

    Say I have a template layout saved in template.html. This template includes a banner, side navigation, content container, and footer. Can I use flask to break up these page elements in such a way that I can have files such as banner.html,…
    Michael
    • 2,031
    • 6
    • 21
    • 27