Jinja Partials

0.3.1 · active · verified Sun Apr 12

Jinja Partials is a Python library designed for the simple reuse of partial HTML page templates within the Jinja template language, particularly for Python web frameworks. It addresses the 'long template problem' by enabling function-like reusability for HTML fragments. The library is currently active, with its latest version being 0.3.1, and shows a consistent release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `jinja-partials` with a Flask application. It shows how to register the extension and use the `render_partial` function in your templates. Create an `index.html` and a `partials/greeting.html` inside a `templates` directory, then run the Flask app.

import flask
from jinja_partials import register_extensions
import os

app = flask.Flask(__name__)

# Assuming templates are in a 'templates' directory and partials in 'templates/partials'
register_extensions(app)

@app.route('/')
def index():
    return flask.render_template('index.html', title='Home Page', user={'name': 'Alice'})

# Example template structure:
# templates/
#   index.html
#   partials/
#     greeting.html

# index.html content:
# <!DOCTYPE html>
# <html>
# <head><title>{{ title }}</title></head>
# <body>
#   <h1>Welcome!</h1>
#   {{ render_partial('greeting.html', name=user.name) }}
# </body>
# </html>

# partials/greeting.html content:
# <p>Hello, {{ name }} from a partial!</p>

if __name__ == '__main__':
    # In a real app, you'd run with `flask run` or a WSGI server.
    # For this quickstart, we'll simulate rendering directly.
    # You would typically run `app.run(debug=True)`
    print("Quickstart setup complete. Run this with a Flask development server.")

view raw JSON →