Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Andrey Shpak <ashpak@ashpak.ru>
- Loading branch information
1 parent
0b40625
commit c0e7698
Showing
9 changed files
with
178 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
.. _`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' %}", | ||
"_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): | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
.. 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. | ||
|
||
For many cases, this will be unneccessarily complicated. It's likely that we'd only want to register a single function | ||
as a filter. For this, we can use the ``simple_filter`` decorator: | ||
|
||
.. code-block:: json | ||
{ | ||
"project_slug": "Foobar", | ||
"year": "{% now 'utc', '%Y' %}", | ||
"_extensions": ["local_extensions.simplefilterextension"] | ||
} | ||
.. code-block:: python | ||
# -*- coding: utf-8 -*- | ||
from cookiecutter.utils import simple_filter | ||
@simple_filter | ||
def simplefilterextension(v): | ||
return v * 2 | ||
This snippet will achieve the exact same result as the previous one. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"project_slug": "Foobar", | ||
"test_value_class_based": "{{cookiecutter.project_slug | foobar}}", | ||
"test_value_function_based": "{{cookiecutter.project_slug | simplefilterextension}}", | ||
"_extensions": [ | ||
"local_extensions.simplefilterextension", | ||
"local_extensions.FoobarExtension" | ||
] | ||
} | ||
|
1 change: 1 addition & 0 deletions
1
tests/test-extensions/local_extension/local_extensions/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .main import FoobarExtension, simplefilterextension # noqa |
21 changes: 21 additions & 0 deletions
21
tests/test-extensions/local_extension/local_extensions/main.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
"""Provides custom extension, exposing a ``foobar`` filter.""" | ||
|
||
from jinja2.ext import Extension | ||
from cookiecutter.utils import simple_filter | ||
|
||
|
||
class FoobarExtension(Extension): | ||
"""Simple jinja2 extension for cookiecutter test purposes.""" | ||
|
||
def __init__(self, environment): | ||
"""Foobar Extension Constructor.""" | ||
super(FoobarExtension, self).__init__(environment) | ||
environment.filters['foobar'] = lambda v: v * 2 | ||
|
||
|
||
@simple_filter | ||
def simplefilterextension(v): | ||
"""Provide a simple function-based filter extension.""" | ||
return v.upper() |
8 changes: 8 additions & 0 deletions
8
tests/test-extensions/local_extension/{{cookiecutter.project_slug}}/HISTORY.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
History | ||
------- | ||
|
||
0.1.0 | ||
----- | ||
|
||
First release of {{cookiecutter.test_value_class_based}} on PyPI. | ||
{{cookiecutter.test_value_function_based}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 comment
on commit c0e7698
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we already use local extensions after pip installing cookiecutter?
The description is wrong here, because not the
main.py
is searched, but a module as far as I can see. In the test also the examples are provided via the__init__.py
.Furthermore, for small extensions a
local_extensions.py
would also work, this is also mentioned in #1240 (comment), but it was not adapted in the PR.