Skip to main content

New hybrid file format combining a Jinja template with Python code in a frontmatter

Project description

Jinjapy

New hybrid file format combining a Jinja template with Python code in a frontmatter.

Installation

pip install jinjapy

Usage

A jinjapy file is a standard python module which is attached to a jinja template.

A folder containing multiple jinjapy files is registered as a python package using register_package(). This returns a Jinja loader than can be used to create a Jinja environment.

from jinja2 import Environment
import jinjapy

loader = jinjapy.register_package("my_package")
env = Environment(loader=loader)

# execute the module + render the template
# the module globals are used as the template context
template_output = jinjapy.execute_module(env, "my_package.module")

# module is available like any other import
import foo from my_package.module

my_package/module.jpy:

---
foo = "bar"
---
{{ foo }}

Specification

A jinjapy file contains 2 sections:

  • A frontmatter with some Python code (enclosed by lines containg 3 dashes "---")
  • A body containing some Jinja template code

Both are optional:

  • If the frontmatter is missing, the file only contains a Jinja template. It won't be available as a python module.
  • If the frontmatter is left unclosed (the file starts with "---" on a single line followed by some python code), the file has no template

Editor support

A VS Code extension is available to add syntax highlighting for jinjapy files.

Syntax highlighting

A pyment lexer is provided, named "jinjapy" or "jpy". It will also match .jpy files.

How does it work

A custom package finder and package loader is added to python search path. The loader extracts the python code from the frontmatter on load.

The returned Jinja loader does the same for the jinja template: it removes the frontmatter and only returns the remplate code.

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

jinjapy-0.2.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jinjapy-0.2.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file jinjapy-0.2.0.tar.gz.

File metadata

  • Download URL: jinjapy-0.2.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for jinjapy-0.2.0.tar.gz
Algorithm Hash digest
SHA256 260c3000403b14506693b2f41710e2ff086c4325ed3dc2cc3d6f0cd0e7a998a6
MD5 d3f7b37e3ba196a6c5e3dcdd90d582b2
BLAKE2b-256 040c4fa62493c4c7c2bbc8c2bffbcedb225c05eb51c3215471029dc1583625dc

See more details on using hashes here.

File details

Details for the file jinjapy-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: jinjapy-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.17

File hashes

Hashes for jinjapy-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87524183ed9e47718bf602dd97bbc32c1cf3a4f687f8a6ea5d6c564b389e88f9
MD5 4521908d9a3207ce088ea902842b348c
BLAKE2b-256 3c24e19ce6258dc25f7dcd52d8188a411e74508c6fd19d369f619b1d5147c3b4

See more details on using hashes here.

Supported by

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