Cookiecutter: Inject current datetime into Jinja environment

Created on 26 Feb 2014  路  10Comments  路  Source: cookiecutter/cookiecutter

Add the current datetime to the Jinja2 environment so that it can be rendered in templates.

e.g.
{{ cookiecutter.current_time }}

Might also want config vars for specifying local vs. UTC, etc.

See also #154

enhancement

Most helpful comment

:clock10:
1.4.0 now includes hackebrot/jinja2-time.

All 10 comments

@audreyr

it's a great feature

I need something like:

{{ cookiecutter.current_date }}

to auto-generate first version of CHANGELOG file

Thanks for the feedback @khorn and @luzfcb.

If possible, I think something like #154 to address this would be better than hard-coding current_time and current_date. Although I could see both as possibilities.

Generally having a way to use custom Jinja2 filters (extensions too?) with Cookiecutter might be the way to go about #154. I'll explore it when I can, but I've got a lot on my plate before I get to this.

The idea here is to provide a datetime object with the date/time that the cookiecutter tool is run to the template environment. The changes discussed in #154 could be used to format such an object, but you still need to inject it into the template context, which to me is a separate issue.

I wonder if maybe there shouldn't be some established convention for making it obvious that a variable in the cookiecutter namespace has been automatically injected rather than created from the cookiecutter.json context file.

So perhaps instead of:
{{ cookiecutter.current_datetime }}

(or whatever we decide to call it)

you might have:
{{ cookiecutter._current_datetime }} (prefixed with underscore)

or:
{{ cookiecutter.current_datetime }} ("dundered")

Thoughts?

I'm also not crazy about the "current_datetime" name though I suggested it and variants thereof. We want to make it clear that it represents the time at which the template was rendered. What's the best way to express that?

Suggestions for names?

:+1:

I think this is a really useful feature. Off the top of my head, it allows cookiecutter bakers (authors of cookiecutter templates) the ability to timestamp when a project is generated. I'm sure other people can come up with other use cases.

To make it clear it's generated, why not use a different namespace than cookiecutter? Maybe just generated? And to make it clear it's the rendering time, {{ generated.rendered_datetime }}. Explicit is better than implicit and all that ;-)

I like the cookiecutter namespace, as it makes it clear at a glance that a template is a cookiecutter template. Which prompts the following question: Is there something that is both explicit and follows a baking theme?

Or maybe a second namespace for values that cookiecutter automatically makes available?

Ok, so when you're baking cookies, you're kind of assumed to have a cookie sheet right? So maybe for values that are just assumed to exist, we could use cookiesheet?

So, you might have cookiesheet.rendered_datetime, or cookiesheet.rendered_at or something.

I wonder what other things might be useful in such a namespace...

...but that's a discussion for another day (and issue).

It's obviously too late for me to be githubbing again...

Or if you wanted a baking theme for the name of the datetime...how about

cookiecutter.ding! (that's the timer on the oven letting you know it's time to take the cookies out)

...

Right. Going to bed now.

:clock10:
1.4.0 now includes hackebrot/jinja2-time.

Was this page helpful?
0 / 5 - 0 ratings