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

Filter list of dictionariers and print value in Ansible/Jinja

I can't get my head around this, although it should be a trivial problem. I'm using Ansible (and Jinja templating) to build a template and I have a list of dictionaries like (here displayed as JSON): "datacenters": [{ "description": "Main…
ColOfAbRiX
  • 1,039
  • 1
  • 13
  • 27
3
votes
3 answers

Run command only once if any items in a list are True (Ansible)

I'd like to stop Nginx just once if any of the items in a list are True. Because multiple items in the list could be True, I only want to stop Nginx on the first and then not run this command again. At a later point in the script I will use the…
tim_xyz
  • 11,573
  • 17
  • 52
  • 97
3
votes
0 answers

Using url_for while streaming template from Flask raises RunTimeError

I am rendering a template by streaming data from a Flask view. When I do not use url_for, the page works correctly. If I add url_for, I get RuntimeError: Attempted to generate a URL without the application context being pushed. This has to be…
Jonas
  • 1,473
  • 2
  • 13
  • 28
3
votes
1 answer

yaml and jinja2 reader

I would like to be able to read in python a YAML jinja configuration file like using the PyYAML library but I'm receiving errors: {% set name = "abawaca" %} {% set version = "1.00" %} package: name: {{ name }} version: {{ version…
ypriverol
  • 585
  • 2
  • 8
  • 28
3
votes
1 answer

Get list of IPs of hosts in group with Jinja2 filter

I'm building a couple of roles where I need to give the config the IP addresses of all hosts in a specified group as a comma separated string. Is there a jinja2 combination of filters (like map, match, select, join etc) that produce the same as…
Fredrik Rambris
  • 316
  • 3
  • 10
3
votes
2 answers

Finding the sum of numbers in jinja2 template using flask

I am learning flask and tried to create a web app that accepts a student's name and marks, finds the total of all the marks and display things in a table. But the total always gets displayed as 0. The code is given below mark_total.py: from flask…
Karthick S
  • 3,204
  • 6
  • 36
  • 52
3
votes
1 answer

Using {{ and }} in ansible shell command

I have this in my playbook: - name: Get facts about containers shell: "docker ps -f name=jenkins --format {%raw%}{{.Names}}{% endraw %}" register: container Note, that I inserted the {%raw%} and {%endraw%} so that ansible does not evaulate the…
Nathan
  • 7,099
  • 14
  • 61
  • 125
3
votes
1 answer

django 1.10 one app page with a link redirect to another app page

I'm new to django server and trying to build a simple website for user registration. Here is the problem, I create my own app with index.html as my homepage. I also used another user registration app from:…
Minwu Yu
  • 311
  • 1
  • 6
  • 24
3
votes
1 answer

Flask App: Load images from current working directory

tl;dr version: how do I get my Flask app to load images not in the project directory? I am building (as a fun project) a Flask app, designed as a command-line app, that loads a random image from my current working directory in the browser. Here is…
ericmjl
  • 13,541
  • 12
  • 51
  • 80
3
votes
1 answer

Call JavaScript function in jinja for loop

I have an HTML page, In the variable schedule has sequential decimal number in seconds. My purpose is create a function to convert all these numbers in time using JavaScript/jQuery, but i could not understand, how can I invoke my function to…
user8303487
  • 31
  • 1
  • 2
  • 4
3
votes
0 answers

Apostrophes converted to ' when sending variable to Javascript

I sent in the following array of dictionaries through Python to HTML: [{'color': '#CFCFC4', 'day': 7}, {'color': '#FDFD96', 'day': 8}] which then gets sent to a Javascript function using the following line: generateHTML("{{ colors }}"); However,…
xilinzhou
  • 33
  • 6
3
votes
1 answer

jinja : using variable from for loop inside an if statement

I am trying to use a variable in a "if" statement, this variable is defined in a for loop, but I cannot get it to works, here is a code excerpt : {% for dir in ['temp','backup','sbin','logs','apps'] %} {% if {{ dir }} == 'temp' %} /tree/{{ dir…
Pier
  • 618
  • 2
  • 8
  • 23
3
votes
3 answers

Ansible: Extract first three octects from an IP address

I have a string with IP addr: 192.168.10.2 I want to extract first three octets of the IP in Ansible and I tried to use this regex. {{comp_ip | regex_replace("^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}"), "//1"}} This does not yield any result. Can someone…
spamulap12
  • 97
  • 1
  • 9
3
votes
1 answer

Flask+AJAX+Jquery+JINJA to dynamically update HTML Table

I want to display the Port status dynamically. I don't want to reload the page to see a new value. I know how to get the Port status in Python(using uiApi()). Right now I render a template with the value and show the values in HTML table. How can I…
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
3
votes
1 answer

Jinja2 Frame Variables in Global Function

I have a global function test from jinja2.utils import contextfunction @contextfunction def test(context): context.get_all() And in my test I'm calling it like this... {% set i = 0 %} {% for j in range(0, 10) %} {% set k = 0 %} {{ test()…
Shaun
  • 3,777
  • 4
  • 25
  • 46