HTMX + Flask: Modern Python Web Apps, Hold the JavaScript Transcripts
Chapter: Course conclusion and review
Lecture: Jinja-partials package
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
One of my favorite things to come out of this course is the partials package.
0:05
Remember we did render_partial, and we would give some small part of HTML and some additional data right inside of one of our HTML templates.
0:13
I really like this because even if you're not doing htmx, it's a really cool way to break apart a huge,
0:19
complicated template into small building blocks. Just like you would not really want to write a 1000 line long function,
0:27
it would be way better to write 10 100 line ones or maybe even more short ones. It helps you understand what's going on.
0:34
And these partials both add to reusability, as well as to maintainability along those lines
0:40
So really useful in general. Now it's especially important as we saw in htmx because so often we want to show the whole page and the page might
0:49
contain some HTML, and then based on the server interaction, the server needs to return in another instance maybe with other data,
0:57
basically a subset of that HTML again. And that's exactly what partials let us do over and over throughout this course.
1:05
Just to remind you, let's do one quick review of using one of these partials. By installing the package, and then at the app startup we have to say
1:14
jinja_partials.register_extensions, pass it the app. And then magically, well through the magic of Flask and Jinja,
1:21
this render_partial function will be globally available within all the Jinja templates.
1:26
So here we just use double curly to say take whatever text you get and put it out here, and then we call render_partial,
1:33
give it an HTML link to a partial. Here we have videos/partias/add_button.html. Of course contained in the template folder.
1:42
And then we pass along the data. Like here we have the data we're working with, which is a category that has a category
1:48
field. What the template needs, what the partial needs is something called cat_name. So we say cat_name equals that value.
1:55
Just like a function call, and then when we call render_partial, It's going to go to this add button HTML and here you can see the data
2:02
being passed from the outer template down to the inner template. Really nice and reasonable. Basically functions for templates.