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