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

Python: Multipart html email is sent both with embedded image and attachment

I have checked many posts about it, but still cant find a solution. I am able to send email with embedded pictures, but the email also includes those pictures as attachments, and I need only embedded pictures. I have tried many variations, with…
Yuliya
  • 163
  • 1
  • 2
  • 6
3
votes
1 answer

Loop through array in Chef Template (ERB)

I'm new to chef and starting to learn the ropes and want to know if the below is possible and how to achieve it. I'm coming from someone who's been using ansible for the past 2 years. I want to know how to manipulate .erb templates ansible code -…
moh_abk
  • 2,064
  • 7
  • 36
  • 65
3
votes
1 answer

Get list of variables from Jinja2 template (parent and child)

I'm trying to get a list of variables from a Jinja2 template. test1.j2: some-non-relevant-content {{var1}} {% include 'test2.j2' %} test2.j2: another-text {{var2}} I can get variables from test1 easily: env =…
secret
  • 61
  • 6
3
votes
1 answer

I'm trying to create an url_for in an html tag where the filename for the image is stored in a variable. How would I do this?

I am trying to create a page for users that will display their profile pic and I've retrieved the picture filename from a database and stored that filename as a variable called "profilePic". I've passed profilePic to the html page as a variable, but…
RobertB
  • 33
  • 1
  • 1
  • 3
3
votes
0 answers

Using images in CSS with Jinja and Django

I am making my first blog with Django, everything is working fine, but I can seem to load my burger img with CSS in my blog_app. …
Harton
  • 106
  • 2
  • 9
3
votes
1 answer

Python Jinja2 macro whitespace issues

This is kind of extension to my other question Python Jinja2 call to macro results in (undesirable) newline. My python program is import jinja2 template_env = jinja2.Environment(trim_blocks=True, lstrip_blocks=True, autoescape=False,…
3
votes
1 answer

template blocks start or end with "{%-" or "-%}" symbols

I'm reading "flask_bootstrap/templates/bootstrap/base.html" on GitHub, and find some block symbols strange, like {%- block ... %} or {% endblock ... -%}. Why there is "-" before or after {% or %}? Will it make the block declaration different than…
ping
  • 33
  • 4
3
votes
1 answer

Ansible: Appending to list with variable files

Is there a way to have multiple variable files append to an array? The end goal is to have each file append an AWS security group for the launch configuration without the need to copy those groups into each…
MANCHUCK
  • 2,424
  • 1
  • 16
  • 22
3
votes
0 answers

How to write a simple extension with variable arguments?

I would like to implement a custom extension with jinja2 that allow dynamical loading of an external datasource. For example: {# A jinja template #} {% datasource '../foo.yml' %} {{ foo }} {{ bar }} So I wrote this: #!/usr/bin/env python from…
nowox
  • 25,978
  • 39
  • 143
  • 293
3
votes
3 answers

Including jinja from pillar from salt-stack

I wonder if it is possible to include jinja from a pillar from salt-stack? thanks
roman
  • 607
  • 2
  • 10
  • 18
3
votes
3 answers

Length of list in jinja template, where the list is the result of a DBSession.query

How do I find the length of a list of objects in a jinja template when the list of objects has been created by querying a database? I thought There are {{ items|length }} items in this category. would work, but items is a result of: items =…
Simon Otter
  • 173
  • 1
  • 3
  • 13
3
votes
3 answers

Django rendering display name of choice field in form

tl;dr: How do I make a form output the ‘nice’ name of the choices in a model? I have a Django model with choices, defined like this: class City(models.Model): AMSTERDAM = 'AMS' ROTTERDAM = 'ROT' THE_HAGUE = 'THE' UTRECHT = 'UTR' …
Flobin
  • 626
  • 1
  • 10
  • 26
3
votes
1 answer

How to remove line when variable is not defined in jinja2 template

I have a simple jinja2 template: {% for test in tests %} {{test.status}} {{test.description}}: {{test.message}} Details: {% for detail in test.details %} {{detail}} {% endfor %} {% endfor %} Which work really good…
Konrad Klimczak
  • 1,474
  • 2
  • 22
  • 44
3
votes
2 answers

Why do trim_blocks and lstrip_blocks under Flask/OpenShift produce different results when tested online, compared when to tested locally?

I'm writing a simple web app using Flask, which is hosted on OpenShift. I want to control whitespace in my template files, with the Jinja command lines essentially removed as in:
{% if True %} yay {% endif %}
...producing:
3
votes
2 answers

How to set the default option for a select field using Jinja templates

I need to add default option value to a select field using Jinja templates. form.py class TeamForm(Form): user = SelectField(u'Team Member') views.py class myview(request,id): form = TeamForm(request.form) members…
neelima
  • 461
  • 10
  • 25