A simple way to render a Jinja2 template within another Jinja2 template.
Project description
Jinja Tools
A tool to help you logically separate how things are rendered in your project. For example, you may want to have smaller templates rendered within larger templates.
Register the Blueprint
To enable the features of the package, you must register the flask blueprint.
from flask import Flask import jinja_subrender app = Flask() app.register_blueprint(jinja_subrender.bp)
Sub-Render Example
You may have a jinja-template that displays a collection of items, called ‘all_items.jinja’. You may also have a jinja-templte for how each item should be rendered, called ‘item.jinja’.
In this case your ‘all_items.jinja’ template could look something like:
{% extends "main.jinja" %} {% block content %} {% for item in items %} <div> {{ item | Render('item.jinja') }} </div> {% endfor %} {% endblock %}
The ‘item.jinja’ template could look something like:
<div> {{ obj['name'] }} <img src="{{ obj['src'] }}"/> </div>
Note: Render() always passes the item as ‘obj’, but you can rename within the jinja template, like so:
{% set item = obj %} <div> {{ item['name'] }} <img src="{{ item['src'] }}"/> </div>
Passing multiple arguments
You can also pass multiple arguments by passing them as a tuple in the super-template. For example, ‘all_items.jinja’ could look like:
{% set total = items | length %} {% for item in items %} <div> {{ (item, total) | Render('item.jinja') }} </div> {% endfor %}
You can then access the second argument in the sub-template by breaking it apart with ‘set’
{% set item, total = obj %} <div> {{ item['name'] }} <img src="{{ item['src'] }}"/> total = {{ total }} </div>
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file jinja_subrender-0.0.1.tar.gz
.
File metadata
- Download URL: jinja_subrender-0.0.1.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4831624b860a5ae6961b02fa8f142d64992d738c9dfe51d86e8f05919b6926da |
|
MD5 | 285153fcd7ab140c6275de5dccc71af5 |
|
BLAKE2b-256 | c6a0d1660457c25a22eda680d368114f9f27134e6212ae1ff9193b542bafb9b6 |
File details
Details for the file jinja_subrender-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: jinja_subrender-0.0.1-py3-none-any.whl
- Upload date:
- Size: 2.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77120cd9592f4d7aed8b29ebb65e6b841110bf7a35493ddcce962b60cd698b3a |
|
MD5 | 4664ad64661963dfd01c5b33970a3d69 |
|
BLAKE2b-256 | ea9e7d6adf2d1ce1401026786b34319a9b1cc04bfd0ed45b1af492b83380d6ed |