New hybrid file format combining a Jinja template with Python code in a frontmatter.
pip install jinjapy
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.modulemy_package/module.jpy:
---
foo = "bar"
---
{{ foo }}
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
A VS Code extension is available to add syntax highlighting for jinjapy files.
A pyment lexer is provided, named "jinjapy" or "jpy". It will also match .jpy files.
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.