Special Jinja2 extension for Copier that allows to load extensions using file paths relative to the template root instead of Python dotted paths.
Project description
Copier Templates Extensions
Special Jinja2 extension for Copier that allows to load extensions using file paths relative to the template root instead of Python dotted paths.
Requirements
Copier Templates Extensions requires Python 3.6 or above.
To install Python 3.6, I recommend using pyenv
.
# install pyenv
git clone https://github.com/pyenv/pyenv ~/.pyenv
# setup pyenv (you should also put these three lines in .bashrc or similar)
export PATH="${HOME}/.pyenv/bin:${PATH}"
export PYENV_ROOT="${HOME}/.pyenv"
eval "$(pyenv init -)"
# install Python 3.6
pyenv install 3.6.12
# make it available globally
pyenv global system 3.6.12
Installation
With pip
:
pip install copier-templates-extensions
With pipx
:
pip install --user pipx
pipx install copier
pipx inject copier copier-templates-extensions
Usage
In your template configuration, first add our loader extension, then add your templates extensions using relative file paths, and the class name after a colon:
_extensions:
- copier_templates_extensions.TemplateExtensionLoader
- extensions/context.py:ContextUpdater
- extensions/slugify.py:SlugifyExtension
Context hook extension
This package also provides a convenient extension class allowing template writers to update the context used to render templates, in order to add, modify or remove items of the context.
In one of your relative path extensions modules,
create a class that inherits from ContextHook
,
and override its hook
method:
from copier_templates_extensions import ContextHook
class ContextUpdater(ContextHook):
def hook(self, context):
new_context = {}
new_context["say"] = "hello " + context["name"]
return new_context
Using the above example, your context will be updated
with the new_context
returned by the method.
If you prefer to modify the context in-place instead,
for example to remove items from it,
set the update
class attribute to False
:
from copier_templates_extensions import ContextHook
class ContextUpdater(ContextHook):
update = False
def hook(self, context):
context["say"] = "hello " + context["name"]
del context["name"]
How does it work?
Beware the ugly hack!
Upon loading the special loader extension,
the function responsible for importing
a Python object using its dotted-path (a string)
is patched in the jinja.environment
module,
where it's used to load extensions.
The patched version adds support
for loading extensions using relative file paths.
The file system loader of the Jinja environment
and its searchpaths
attribute are used to
find the local clone of the template and determine
the absolute path of the extensions to load.
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
Hashes for copier-templates-extensions-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1adfbf65dc3b300f96506083c285a0f8df9dc65127a35c29cb32c4d2af040e13 |
|
MD5 | aae108f68378aaf3828eca29109369b5 |
|
BLAKE2b-256 | 96d21dec1ddd72de6f651d74e3bb0a616d4ef225de5bff3da355ed56d2bb8e20 |
Hashes for copier_templates_extensions-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f623213be9684383623a009f5c2853f2361005d841b541e803fc0d10a5e5123 |
|
MD5 | 4138e9f4a75d429453b7eb2ab6761cd2 |
|
BLAKE2b-256 | ca430dc7759216603be625296b1f1704fe42da9a19040dbf798f665c7477fadf |