Skip to content

Commit

Permalink
document local_extensions feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mwesterhof committed Feb 25, 2020
1 parent e713502 commit e950ef9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/advanced/index.rst
Expand Up @@ -21,3 +21,4 @@ Various advanced topics regarding cookiecutter usage.
dict_variables
template_extensions
directories
local_extensions
36 changes: 36 additions & 0 deletions docs/advanced/local_extensions.rst
@@ -0,0 +1,36 @@
.. _`template extensions`:

Local Extensions
----------------

*New in Cookiecutter X.x*

A template may extend the Cookiecutter environment with local extensions.
These can be part of the template itself, providing it with more sophisticated custom tags and filters.

To do so, a template author must specify the required extensions in ``cookiecutter.json`` as follows:

.. code-block:: json
{
"project_slug": "Foobar",
"year": "{% now 'utc', '%Y' %}",
"_local_extensions": ["local_extensions.FoobarExtension"]
}
This example assumes that a ``local_extensions`` folder (python module) exists in the template root.
It will contain a ``main.py`` file, containing the following (for instance):

.. code-block:: python
# -*- coding: utf-8 -*-
from jinja2.ext import Extension
class FoobarExtension(Extension):
def __init__(self, environment):
super(FoobarExtension, self).__init__(environment)
environment.filters['foobar'] = lambda v: v * 2
This will register the ``foobar`` filter for the template.

0 comments on commit e950ef9

Please sign in to comment.