jinjarope¶
jinjarope is a toolkit for Jinja2: an enhanced
Environment with a large set of extra filters, tests and globals, loaders for
fsspec filesystems and nested dicts, composable
loader classes, and TOML-based "JinjaFiles" to declare filters / tests / environment config.
Installation¶
Optional extras: cli (command line interface), extras (formatting, highlighting, humanize,
natsort filters), httpx2 (alias httpx), icons (icon filters), llm (llm_complete
filter via agentpool), yaml.
Quickstart¶
import jinjarope
env = jinjarope.Environment(loader=jinjarope.DictLoader({"hello.jinja": "Hello {{ name }}!"}))
print(env.render_template("hello.jinja", name="World")) # Hello World!
print(env.render_string("{{ 'Hello World' | slugify }}")) # hello_world
Features¶
jinjarope.Environment: drop-injinja2.Environmentsubclass with ~150 additional filters / tests / functions,render_string/render_file/render_template,with_globals(), selectable undefined behavior ("strict","lax", ...), and an entry-point (jinjarope.environment) to let other packages extend every environment.- Subclasses of all jinja2 loaders with
__eq__,__hash__,__repr__and__getitem__, plus fsspec-based and dict-based loaders. - JinjaFiles: TOML files declaring filters, tests, functions and environment config.
- A read-only fsspec filesystem exposing a loader's templates (for debugging).
- A
jinjaropeCLI to render (remote) templates:jinjarope -i template.jinja -j config.toml -o out.md(with thecliextra).
FsSpecFileSystemLoader¶
Works like a FileSystemLoader, but on any fsspec-supported (remote) location.
Using the dir:: prefix, any folder can be set as root.
import fsspec
import jinjarope
# protocol path
loader = jinjarope.FsSpecFileSystemLoader("dir::github://phil65:jinjarope@main/tests/testresources")
env = jinjarope.Environment(loader=loader)
env.get_template("testfile.jinja").render()
# protocol and storage options
loader = jinjarope.FsSpecFileSystemLoader("github", org="phil65", repo="jinjarope")
env = jinjarope.Environment(loader=loader)
env.get_template("README.md").render()
# fsspec filesystem
fs = fsspec.filesystem("github", org="phil65", repo="jinjarope")
loader = jinjarope.FsSpecFileSystemLoader(fs)
env = jinjarope.Environment(loader=loader)
env.get_template("README.md").render()
FsSpecProtocolPathLoader¶
Accepts complete fsspec protocol paths as template names.
loader = jinjarope.FsSpecProtocolPathLoader()
env = jinjarope.Environment(loader=loader)
env.get_template("github://phil65:jinjarope@main/tests/testresources/testfile.jinja").render()
NestedDictLoader¶
import tomllib
with open("templates.toml", "rb") as toml_file:
content = tomllib.load(toml_file)
loader = jinjarope.NestedDictLoader(content)
env = jinjarope.Environment(loader=loader)
env.get_template("example/template").render(something="value")
Loader composition¶
loader = jinjarope.FileSystemLoader("templates/")
template = loader["path/to/template.jinja"] # jinja2.Template
source = loader.get_template_source("path/to/template.jinja")
# OR loaders to get a ChoiceLoader
choice_loader = jinjarope.FileSystemLoader("templates/") | jinjarope.PackageLoader("my_package")
# pathlib-style division creates a PrefixLoader
prefix_loader = "path_prefix" / jinjarope.FileSystemLoader("templates/")
Filters, tests and functions¶
See the documentation for the full list of built-in filters, tests and functions with examples.