Template engines
MkNodes renders its Jinja templates (node files, MkTemplate, render_jinja texts, the
Jinja pass of generated pages) with Jinja2 by default. minijinja, a Rust
implementation of Jinja, can be used instead:
pip install "mknodes[minijinja]"
mknodes build --jinja-engine minijinja
The engine comes from (first one set wins):
- the CLI option
--jinja-engineofbuild,serveandgenerate, jinja-engine = "minijinja"in[tool.mknodes]ofpyproject.toml(or themknodesentry in the plugins list ofmkdocs.yml),- the environment variable
MKNODES_JINJA_ENGINE(also used by nodes created outside a site build, e.g. in tests), jinja2.
Both engines come from jinjarope, which lists the differences. All filters, globals and node classes are the same; templates of MkNodes itself render the same with both. What to watch for in your own templates:
- Attributes starting with
_are hidden fromobj.attr: use thegetattrfilter ({{ cls | getattr("__name__") }}), it works with both engines. mapping.key(...)calls a method: mappings need a__getattr__(mk.MkText(...),_mk.MkText(...)work).- Strings are minijinja strings with a subset of Python’s
strmethods (noremovesuffix: use the filter of the same name);"%s" % xis not supported. - Node metadata (
created.source_filename) of nodes created in templates points to the Python code that rendered the template (templates have no Python frames).
Page info
Code for this page
mknodes.manual.templating_section._
@nav.route.page("Template engines")
def _(page: mk.MkPage) -> None:
page += mk.MkTemplate("templating/template_engines.jinja")
Resources
Resources(css=[],
markdown_extensions={},
plugins=[],
js=[],
assets=[],
packages=[])
Metadata
created:
source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/templating_section.py
source_function: _
source_line_no: 60
title: Template engines