Skip to main content

Render Jinja2 template block as HTML page fragments on Python web frameworks.

Project description

Jinja2 fragments

Jinja2 Fragments allows rendering individual blocks from Jinja2 templates. This library was created to enable the pattern of Template Fragments with Jinja2. It's a great pattern if you are using HTMX or some other library that leverages fetching partial HTML.

With jinja2, if you have a template block that you want to render by itself and as part of another page, you are forced to put that block on a separate file and then use the include tag (or Jinja Partials) on the wrapping template.

With Jinja2 Fragments, following the Locality of Behavior design principle you have a single file for both cases. See below for examples.

Install

It's just pip install jinja2-fragments and you're all set. It's a pure Python package that only needs jinja2 (for obvious reasons!).

Usage

This is an example of how to use the library with vanilla Jinja2. Given the template page.html.jinja2:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>This is the title</title>
</head>
<body>
    <h1>This is a header</h1>
    {% block content %}
    <p>This is the magic number: {{ magic_number }}.</p>
    {% endblock %}
</body>
</html>

If you want to render only the content block, do:

from jinja2 import Environment, FileSystemLoader, select_autoescape
from jinja2_fragments import render_block

environment = Environment(
    loader=FileSystemLoader("my_templates"), 
    autoescape=select_autoescape(("html", "jinja2"))
)
rendered_html = render_block(
    environment, "page.html.jinja2", "content", magic_number=42
)

And this will only render:

<p>This is the magic number: 42.</p>

Usage with Flask

If you want to use Jinja2 Fragments with Flask, assuming the same template as the example above, do:

from flask import Flask, render_template
from jinja2_fragments.flask import render_block

app = Flask(__name__)

@app.get("/full_page")
def full_page():
    return render_template("page.html.jinja2", magic_number=42)


@app.get("/only_content")
def only_content():
    return render_block("page.html.jinja2", "content", magic_number=42)

How to collaborate

This project uses pre-commit hooks to run black, isort, pyupgrade and flake8 on each commit. To have that running automatically on your environment, install the project with:

pip install -e .[dev]

And then run once:

pre-commit install

From now one, every time you commit your files on this project, they will be automatically processed by the tools listed above.

How to run tests

You can install pytest and other required dependencies with:

pip install -e .[tests]

And then run the test suite with:

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jinja2_fragments-0.1.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

jinja2_fragments-0.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file jinja2_fragments-0.1.1.tar.gz.

File metadata

  • Download URL: jinja2_fragments-0.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for jinja2_fragments-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9917a201ae9e76ea9f1299093c78578d4718adb4f09642d744c0362b1ef02a7f
MD5 0291bf30782aa9732b0142f3ae2bd3e4
BLAKE2b-256 492e60522f7c6042a2221535a35461be4c5d3cc7be37cf110f497acd8c6154bb

See more details on using hashes here.

File details

Details for the file jinja2_fragments-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for jinja2_fragments-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b168f3512e57aa9884d2966c2e5b6e038c17e43eadf4e78bea297b8afef269f3
MD5 b78d95e99e7d374e4657b18890372eb1
BLAKE2b-256 194d05a17c799189eedb0768c7ef45874ce8adb44ef70b13433bcc632d1ecb94

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page