HTMX + Flask: Modern Python Web Apps, Hold the JavaScript Transcripts
Chapter: HTML template partials
Lecture: Installing jinja-partials

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Well let's go and get this Jinja Partials package installed and integrated. Here we are on
0:05 GitHub. The public repo, this is open source as of course you would imagine. Scroll down a little bit here.
0:13 We, there's an example we saw. To install it, we just need to pip install jinja-partials.
0:19 So we're going to put that over in our code. Notice we've moved on to chapter five, so code, chapter five, partials and then chapter five final.
0:30 Just like before what we're starting with here and then the final version, both in that repo for you.
0:36 Now I could come down here, and I could just pip install this but that doesn't work well for the longevity of our project here.
0:44 What we'd be better off doing is putting it here into our requirements file and immediately PyCharm says, oh, we need to install that.
0:51 So let's go and do this. Boom, it's installed. Okay, now, what do we need to do?
0:57 Well, the partials thing, one of the things that tries to make really easy for you is for, just to basically become a natural feature of Jinja itself.
1:07 The last thing you want to do is have to pass a bunch of information over
1:11 to each template or write a little bit of fragment of Python to make this possible So all you have to do is run this one line here,
1:19 jinja_partials.register_extensions and pass the app at startup, and then you can use it like we saw in that example. So let's go and do that as well.
1:28 Here's our app.py and notice it has this section about configuring the template operations. So if we go down here,
1:36 there's a few things that I was already passing over, for example, I would like to be able to use the len function or isinstance
1:43 or str or give me the type of something inside of Jinja. Normally you can't do that, but if you run this bit of code you can.
1:51 Let's go and do our other things. So we're going to say, we want jinja_partials and
1:56 we've got to import that. And then we want to say register_extensions, I want to pass in the app, or not
2:03 pass into this right, grabbing the global one, but we're doing it here and now we should be able to use this functionality within our page.
2:11 So for example, We'll now be able to just call render_partial and pass some
2:17 partial view, some partial Jinja template and then all the data that it needs. So basically keyword arguments, just like we do with regular Jinja.
2:28 This is all we gotta do, it's basically set up. Now all we gotta do is just call render_partial where
2:34 we need to use it. One other thing, I talked to people about this package and they said that's neat, but you should probably be using Jinja
2:42 macros, Jinja already has this. It sort of has this, let's have a look. So over here in the Jinja pallets
2:49 project. Notice there is this kind of an idea of creating a function in Jinja.
2:54 Here we can create a macro called input, and it's going to render this. Yes. And then down here, just like render_partial,
3:02 you can say input and give it data. That looks a lot like what this jinja_partials thing is. The difference is, in the
3:10 jinja_partials world, we need our partial, we'll find one, like this, we need this to be directly usable as a template
3:20 right? We don't want to have to define a function and then call the function every time on every page. It would be very weird to do that.
3:29 The ability to directly use that as a standalone page doesn't really make any sense. So while macros are very similar,
3:37 they're not exactly the same thing. I think you could pull it off maybe with macros but not nearly in a such
3:44 a clean and natural way. So jinja_partials, it is for us in this courese.


Talk Python's Mastodon Michael Kennedy's Mastodon