Copier Template Extensions
Copier Template Extensions (copier-template-extensions) is a specialized Jinja2 extension for Copier, enabling the loading of other Jinja2 extensions using file paths relative to the template root, rather than standard Python dotted paths. As of version 0.3.3, it offers improved migration support. The project maintains an active release cadence with periodic updates.
Common errors
-
ModuleNotFoundError: No module named 'copier_templates_extensions'
cause Your `copier.yaml` or Python code still references the old, plural package name (`copier_templates_extensions`) after upgrading to version 0.3.2 or newer.fixUpdate all references in `copier.yaml` and Python code from `copier_templates_extensions` to `copier_template_extensions` (singular). -
Failed to load extension './my_extension.py:my_filter': Could not import 'my_filter' from module 'my_extension' (Jinja2Error)
cause The `copier-template-extensions` library itself was not loaded by Copier, so it cannot resolve relative paths for other extensions specified in `_extensions`.fixEnsure `"copier_template_extensions.CopierExtension"` is explicitly listed as the first item in the `_extensions` key in your `copier.yaml`. -
Error in extension configuration for 'copier_template_extensions.CopierExtension': The 'copier' library version is too old. (CopierInternalError)
cause The installed `copier` version is older than 9.2, which is required by `copier-template-extensions` 0.3.1 and newer.fixUpgrade your `copier` installation to version 9.2 or newer: `pip install --upgrade copier`.
Warnings
- breaking The project name changed from `copier-templates-extensions` (plural) to `copier-template-extensions` (singular) in version 0.3.2.
- breaking Copier Template Extensions now requires `copier>=9.2`.
- gotcha This library enables loading Jinja2 extensions via relative file paths. If you attempt to use a relative path for an extension without first loading `copier_template_extensions.CopierExtension`, Copier will raise a `ModuleNotFoundError` or `TemplateSyntaxError`.
Install
-
pip install copier-template-extensions
Imports
- copier_template_extensions.CopierExtension
copier_templates_extensions.CopierExtension
copier_template_extensions.CopierExtension
Quickstart
# copier.yaml
_extensions:
- "copier_template_extensions.CopierExtension"
# You can now load other extensions using relative paths:
- "./my_template_utils.py:custom_filter"
# my_template_utils.py
def custom_filter(value):
return f"CUSTOM({value.upper()})"
# template.txt.jinja
Hello {{ 'world' | custom_filter }}