Using and Mastering Cookiecutter Transcripts
Chapter: Creating Cookiecutter templates
Lecture: Template extensions

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Remember this cool Cookiecutter template. It has some really interesting things we entered a project name
0:08 and it generate the slug, and it also asks for the release statement, it said wait, that's today, this is actually when I ran it,
0:14 now this is when I wrote the slide, but whatever, this is the date generated as the project or as the template is executed
0:21 to generate a project, so this is not actually a part of Cookiecutter, this capability does not exist in Jinja2 either, in fact,
0:29 what this is an extension to Jinja2 itself which we can plug in to Cookiecutter, let's see how that works.
0:36 Over here, we have a list of extensions for Jinja, you can see it talks about how to add them,
0:42 some internationalization stuff, expressions, loops, with statements, auto escaping extensions, and in fact, there is a whole API to write your own,
0:49 and some people have, so if we go over to GitHub and do a search for Jinja2 extension, you can see there is at least 25 of them on GitHub,
0:58 so some about time, this one we're actually going to use, some about Markdown, some about highlighting code,
1:04 and some about Jinja and some about Django here, things like that. So these extensions are really helpful, and we can use them inside of Cookiecutter.
1:13 Unfortunately, the way they get sort of setup is not super amazing, in order to use these extensions, you basically have to
1:20 install the system, so let's give this a shot and see how it works. So over here, let's look at this first, so what I have done is
1:26 I have put the syntax to generate a year month day expression based on datetime.now, right, so here you can say I'm in a run
1:38 now I'm going to feed it this local time operator here and this is the extension stuff, so if I try to run this, this is not built in anywhere,
1:46 this is an extension that actually comes off of a place called PyPi, the package manager for Python, it's not on my system,
1:54 I can go and try to run it, and it will say no, no, we cannot do this, there is no module named jinja2_time, if I type pip list, you'll see yeah,
2:04 there is Jinja but there is no Jinja time. So here is the bummer deal, if I want to do this,
2:12 if I want to actually have access to these extensions, here is the thing, I have to outside of Cookiecutter pip install jinja2-time
2:22 I'm just going to go and download that, put it in my system, and now if I pip list you can see Jinja2-time
2:29 Okay so that is step 1, the other thing to do is something that we can do for the users, right, we need to come down here
2:35 and say we're going to have an _extensions, I believe that's a list, with the proper assignment there,
2:42 okay, it looks like we're missing a coma up here but, we've got our extensions and our extension is jinja2-time and now we're using it right here,
2:51 notice there is some of these things are underscore that apply to Cookiecutter that are not themselves prompt,
2:57 it obviously won't ask us what extension do we want here, so let's try this, alright, so let's give this a shot, oh woops,
3:03 I still made a mistake here, I need actually not quite like this, I need the .TimeExtension
3:08 There we go, okay, try again, excellent, it looks like it's working, right,
3:12 so my project will be timed project, and it's going to say time-project, beautiful, I'll take that, the creator, I still got to work on this, it's me,
3:22 created on look at that, that is today. So that comes out of our now extension, and because we've installed jinja2-time
3:32 as a package and then over here, we've imported the .TimeExtension, then we're good to go and I think created right now makes a lot of sense,
3:40 and you know what, I am feeling like cyan, boom, done. Now, I don't actually put this value into our files, we didn't reference cookiecutter.created_on
3:49 but if we wanted to it would be the value of that string right there.


Talk Python's Mastodon Michael Kennedy's Mastodon