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
:warning: This is not yet functional. We need something from Copier first: a way to get the path to the template on the disk. For example, Copier could set an environment variable with the path to the temporary cloned template as value.
In your template configuration, first add this extension, then add your templates extensions using relative file paths, and the class name after a colon:
_extensions:
- copier_templates_extensions.Ext
- extensions/context.py:ContextUpdateExtension
- 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 this special 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.
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.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc11dd4158ea85f667e2557a344ca850f4857ac0e3930094cc925bf9b6b9c626 |
|
MD5 | 88d1ad48d381975684028446e3c49af7 |
|
BLAKE2b-256 | 759b5f70fd8d4bef4b27fde6c4fe100e75c96eb50a1fd7ced510dd5f7aa4f8ef |
Hashes for copier_templates_extensions-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dc23b847f189604faf729358f1de6b1d00c0725e61cafced6c69705f208996a |
|
MD5 | 868ac480e95eaa17723f89f73105760c |
|
BLAKE2b-256 | 1957867d88ea774c5bad9991511c0bf13e3456dac3de8a4e1ef0df075ed032e8 |