Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Jinja extensions to be defined/bundled in the template repository #944

Open
wants to merge 1 commit into
base: master
from

Conversation

@ramnes
Copy link
Contributor

ramnes commented May 15, 2017

This commit permits users to define custom Jinja extensions to be used in the cookiecutter.json file.

For example, with something like this in template/helpers.py:

from jinja2.ext import Extension


def get_app_slug(app_name):
    return app_name.lower()


class HelpersExtension(Extension):

    def __init__(self, environment):
        super(Extension, self).__init__()
        environment.filters["get_app_slug"] = get_app_slug

__all__ = ["HelpersExtension"]

It allows to use that Jinja filter directly:

{
    "app_name": "ExampleProject",
    "app_slug": "{{cookiecutter.app_name|get_app_slug}}",
    "_extensions": ["helpers.HelpersExtension"]
}

This is really just a dumb example, but I'm pretty confident that it could be a powerful feature.

I'm kinda new to Cookiecutter, so please point me to the right direction if there's already another way to achieve something like this that I'm not aware of.

@codecov-io

This comment has been minimized.

Copy link

codecov-io commented May 15, 2017

Codecov Report

Merging #944 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #944   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          17     17           
  Lines         684    686    +2     
=====================================
+ Hits          684    686    +2
Impacted Files Coverage Δ
cookiecutter/main.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 53ea509...22e76d1. Read the comment docs.

@ramnes

This comment has been minimized.

Copy link
Contributor Author

ramnes commented Jul 4, 2017

@hackebrot any feedback?

@theodesp

This comment has been minimized.

Copy link

theodesp commented Aug 9, 2017

@ramnes Looks good. Have you got any real cases when you've used Jinja Extensions?

@ramnes

This comment has been minimized.

Copy link
Contributor Author

ramnes commented Aug 9, 2017

Yeah, at numberly we use our fork and we leverage this feature a lot.

We use filters almost everywhere, because it's so convenient to output different things based on a single input.

The example in my first comment is very basic but shows pretty well how we're using it.

@theodesp

This comment has been minimized.

Copy link

theodesp commented Aug 9, 2017

Could you PM the rest of the Team on Gitter about your progress? 👍
https://gitter.im/audreyr/cookiecutter

@ramnes

This comment has been minimized.

Copy link
Contributor Author

ramnes commented Aug 9, 2017

@theodesp I've already done that in July, without much success. 😞

@michaeljoseph

This comment has been minimized.

Copy link
Collaborator

michaeljoseph commented Aug 9, 2017

I would support a more explicit solution to this issue:

  • validate the presence of a well known directory in a template repo (called extensions or similar)
  • add this directory to the import path when we initialise the Jinja environment
  • tests and documentation
@michaeljoseph michaeljoseph self-requested a review Aug 9, 2017
@ramnes

This comment has been minimized.

Copy link
Contributor Author

ramnes commented Oct 9, 2017

This implementation is how I expected cookiecutter to work in the first place, and it's good to have instinctive behaviors working out of the box.

I'd be open to implement it the way you say, but it would take more time, wouldn't be as obvious since you'd have to look at the docs, whereas here it's super simple and just works…

Don't want to keep it simple? What are the downsides?

@ilsken

This comment has been minimized.

Copy link

ilsken commented Oct 28, 2017

I hacked together something to make this available without a fork.

cookiecutter.json:

{
  "repo_name": "test",
  "foo": "bar",
  "_extensions": [ "cookiecutter_repo_extensions:Extension", "ccext:TemplateUtils" ]
}
# template/extensions/ccext.py
# -*- coding: utf-8 -*-

from jinja2.ext import Extension


def split(value, delim=','):
    return [ x.strip() for x in value.split(delim) ]

class TemplateUtils(Extension):
    def __init__(self, environment):
        super(TemplateUtils, self).__init__(environment)
        environment.filters['split'] = split

Only requires you to have cookiecutter_repo_extensions alongside cookiecutter and then all repo local extensions should load fine (as long as this extension is loaded first).

Not pretty and I'm not great at python so forgive the quality. Just a work-around for people who need want this feature but don't want to maintain a fork.

The code here is if you're interested: https://gist.github.com/ilsken/79f3077a72a9113f3e83ae44c8289f77

@ramnes

This comment has been minimized.

Copy link
Contributor Author

ramnes commented Jul 11, 2018

@pydanny gentle ping

As said on gitter two months ago, done is better than perfect, and this is a two lines diff that's sitting here for more than a year now. Can we do something about it?

@ramnes

This comment has been minimized.

Copy link
Contributor Author

ramnes commented Jul 27, 2018

Note to myself: just implement that extension directory idea.

@ramnes ramnes force-pushed the numberly:python-path branch from 22e76d1 to 6b002cd Mar 30, 2019
@codecov-io

This comment has been minimized.

Copy link

codecov-io commented Mar 30, 2019

Codecov Report

Merging #944 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #944   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files          18     18           
  Lines         783    785    +2     
=====================================
+ Hits          783    785    +2
Impacted Files Coverage Δ
cookiecutter/main.py 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3bc7b98...6b002cd. Read the comment docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.