Skip to main content

OrderBlocks Extension for Jinja2

Introduction

This is an extension for the Jinja2 template engine which allows you to select and reorder inheritance blocks.

Example 1: Ordering Specified by Child Template

In this example, a website might wish to publish a number of articles. Each article shows some basic information about the article, the main story itself and perhaps some images. This could be done with a template “article.html” which inherits from the website’s main “layout.html” template and overrides the content block to contain three new blocks: introduction, story and images:

{% extends 'layout.html' %}

{% block content %}
    {% orderblocks article_order %}
        {% block introduction %}
            <ul>
                <li>Some introductory information</li>
                <li>E.g. date, author, &hellip;</li>
            </ul>
        {% endblock %}

        {% block story %}
            <p>Article story goes here &hellip;</p>
        {% endblock %}

        {% block images %}
            <p>
                <img src="..." />
            </p>
        {% endblock %}
    {% endorderblocks %}
{% endblock %}

Note that the blocks are enclosed in the orderblocks tag. This takes one argument, specifying the order in which to show the contained blocks. (Any non-block contents are ignored.) In this case this argument is given by the article_order parameter. When this is undefined, as it is here, (or None) the blocks are shown in their original ordering.

However there might also be some special kinds of articles, such as an image feature article. Here the images should appear in a special display before the story text. The template for this kind of article can inherit from the “article.html” template but specify a value for the article_order parameter:

{% extends 'article.html' %}

{% set article_order = ('introduction', 'images', 'story') %}

{% block images %}
    <!-- Placeholder for fancy image display! -->
    <big>{{ super() }}</big>
{% endblock %}

Example 2: Dynamically-specified Ordering

The block ordering parameter can of course also be specified by a template context parameter. Here a page allows the user to specify which sections they wish to see, and their ordering, via a request argument:

from flask import Flask, render_template, request

app = Flask(__name__)

if 'extensions' not in app.jinja_options:
    app.jinja_options['extensions'] = []
app.jinja_options['extensions'].append(
    'jinja2_orderblocks.OrderBlocks')

@app.route('/example/')
def example_page():
    return render_template(
        'example_template.html',
        article_order=request.args['order'].split(','))

It could be used with a template which included a number of sections, such as the following fragment:

{% orderblocks article_order %}
    {% block x %}
        <section>
            <h2>Section X</h2>
            <p>&hellip;</p>
        </section>
    {% endblock %}

    {% block y %}
        <section>
            <h2>Section Y</h2>
            <p>&hellip;</p>
        </section>
    {% endblock %}

    {% block z %}
        <section>
            <h2>Section Z</h2>
            <p>&hellip;</p>
        </section>
    {% endblock %}
{% endorderblocks %}

The sections could be requested in reverse order via http://…/example/?order=z,y,x or a single section could be requested using http://…/example/?order=y.

Note on Implementation

This extension, as currently implemented, works by replacing the orderblocks tag with a for loop which iterates over the list of requested block names, and includes blocks which match the requested names. In other words it converts a structure like this:

{% orderblocks block_order %}
    {% block x %}
        <p>X</p>
    {% endblock %}

    {% block y %}
        <p>Y</p>
    {% endblock %}
{% endorderblocks %}

into a for loop of if blocks such as:

{% for block_name in block_order %}
    {% if block_name == 'x' %}
        {% block x %}
            <p>X</p>
        {% endblock %}
    {% endif %}

    {% if block_name == 'y' %}
        {% block y %}
            <p>Y</p>
        {% endblock %}
    {% endif %}
{% endfor %}

Installation

The extension can be installed using the setup.py script:

python setup.py install

Before doing that, you might like to run the unit tests:

PYTHONPATH=lib python -m unittest discover

License

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Release files for jinja2-orderblocks 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jinja2-orderblocks 0.1.1
File Size Uploaded
jinja2_orderblocks-0.1.1.tar.gz 17.0 kB Details

Release files / jinja2_orderblocks-0.1.1.tar.gz

Download URL jinja2_orderblocks-0.1.1.tar.gz
Size 17.0 kB
Tags Source
SHA-256 checksum
How to use checksums
acc25ad04f2ca093377a6fcd12a699c14b5fca3dde28b6144bde75a4677e6922
BLAKE2b-256 checksum
How to use checksums
7f394d845b134bd85379ac86d4d747ed26fc28b69ce0aa4686521d06b44eadd8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.9

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page