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.
## 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jinjapy-0.1.2.tar.gz.
File metadata
- Download URL: jinjapy-0.1.2.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e6ec07248bbf90a448a86c0f41e26ee6658a0e925e7c69a99d1c962026cf503b
|
|
| MD5 |
c7ef32087c35ed294831fb440c7e2ac0
|
|
| BLAKE2b-256 |
a3d113a29bcbc194e35fad9094f92e7b0fd66e07c5b0c3e915799d398cdbf3f0
|
File details
Details for the file jinjapy-0.1.2-py3-none-any.whl.
File metadata
- Download URL: jinjapy-0.1.2-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
decb6a53b5c48bac26a5cb5bdf771778c3c5eea43056970513a6f47cca5c4c28
|
|
| MD5 |
e068d8e67de4f29a5de2e1545777688d
|
|
| BLAKE2b-256 |
84e966b7bbe773514bda274a826fd712058b48fa52bb2e176b6cd2829be688d4
|