Skip to main content

jinja2-registry

Project description

jinja2_registry

https://img.shields.io/travis/3ptscience/jinja2-registry.svg MIT License https://img.shields.io/pypi/v/jinja2-registry.svg https://img.shields.io/pypi/pyversions/jinja2-registry.svg

jinja2_registry is a convenience library for managing multiple template namespaces with Jinja2

Example

The following Python code sets up a jinja2.Environment and three distinct jinja2.FileSystemLoader objects under a jinja2.PrefixLoader. It then renders a HTML page based on title.html that includes several other files.

from jinja2_registry import Renderer, register_filesystem_loader

register_filesystem_loader('layouts', 'templates/layouts')
register_filesystem_loader('partials', 'templates/partials')
register_filesystem_loader('pages', 'templates/pages')

renderer = Renderer('pages/title.html')
html = renderer.render()
print(html)

The result is rendered from 4 HTML templates, which are located in different directories. In this example, all of the templates are read from the filesystem; however, users may make use of register_loader to attach any standard Jinja2 loader to the registry.

Result

<!DOCTYPE html>
<html>
  <head>
    <title>new_title</title>
  </head>
  <body>
    <ul>
      <li><a href="/content">Content page</a></li>
      <li><a href="/title">New title page</a></li>
    </ul>
    <div>
      <p>some_content</p>
    </div>
  </body>
</html>

The HTML templates are organized into the following structure. Layouts are separated from partials and content. In a production deployment, layouts would likely be stored separately from pages (e.g., in a library), and partials might be automatically generated.

File structure:

templates/
├── layouts
│   └── base.html
├── pages
│   ├── content.html
│   └── title.html
└── partials
    └── nav.html

templates/layouts/base.html

<!DOCTYPE html>
<html>
  <head>
    <title>{% block title %}default_title{% endblock %}</title>
  </head>
  <body>
    {% include "partials/nav.html" %}
    <div>
      {% block content %}{% endblock %}
    </div>
  </body>
</html>

templates/partials/nav.html

<ul>
  <li><a href="/content">Content page</a></li>
  <li><a href="/title">New title page</a></li>
</ul>

templates/pages/content.html

{% extends "layouts/base.html" %}
{% block content %}
      <p>some_content</p>
{%- endblock %}

templates/partials/title.html

{% extends "pages/content.html" %}
{% block title %}new_title{% endblock %}

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-registry-0.1.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

jinja2_registry-0.1.0-py2.py3-none-any.whl (8.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file jinja2-registry-0.1.0.tar.gz.

File metadata

File hashes

Hashes for jinja2-registry-0.1.0.tar.gz
Algorithm Hash digest
SHA256 298864ed35708c13230d75e4938a14781001bb3d74c29c3f038bb342ca73bd06
MD5 3a2dc2f48a32d270c8886a5d3616c2d0
BLAKE2b-256 c3c407cc94be9aba1b85e1f617f12f2c9fe56eb0a968b3de216932e3a7f6a299

See more details on using hashes here.

File details

Details for the file jinja2_registry-0.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for jinja2_registry-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 692657083ef51c94bd5152eec3f0f1693780b60fa7b9221c4a44aa14ff11756d
MD5 ded4c1c170932c26052367c641c49ab6
BLAKE2b-256 827178f77b5f14fd4ab2057b3fd3e526b17c071f1256f4f5afdbb19461c103e1

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