Chapter: HTML template partials
Lecture: The jinja-partials package
0:00 When we're creating web applications, there's often times we need to duplicate some part of our
0:06 HTML. Here's an example from a project I'm about to tell you about.
0:11 You can see we've got a video,
0:13 we've got a subtitle, and we have the number of views, and we're going to use
0:16 that on the homepage, and we're also going to use that in this listing where we
0:19 can see more of them. They should look kind of familiar,
0:22 huh? Well that is exactly the same thing.
0:25 Yes, it's in a different section, and it probably has a different overall CSS selector
0:31 applied to it, but the HTML is identical.
0:35 And so, why should we write that and maintain it in multiple places?
0:38 We've already seen that in our application.
0:40 Remember the click to add video button?
0:42 It has to appear in two places.
0:44 But I really wanna emphasize that while this is not specifically about htmx,
0:49 it really applies to all web applications.
0:52 They would benefit from having more organization.
0:55 It especially applies to using htmx with any of the Python web frameworks be that
1:01 Flask, Pyramid, Django or some other one like FastAPI.
1:06 So this ability to reuse like a function parts of our HTML in different places is
1:14 really critical. We haven't seen a complicated example of it yet,
1:17 but if we kept going without taking a pause and adopting this package,
1:23 we'd be in big trouble. Speaking of the package,
1:26 here we go, Jinja Partials.
1:28 So this package Jinja Partials, which I created specifically for this course, as well as I
1:33 just realized this problem is not being solved in Jinja or Chameleon or Django templates. All
1:40 the places, that really should be solved better.
1:42 So I created this package specifically for Jinja.
1:45 Notice right under the title there, it says there's also a Pyramid/Chameleon version.
1:49 So if you're using Chameleon, you're not left out.
1:52 We're going to use this Jinja partial package to make our code much cleaner and have
1:58 it defined one time, not in multiple places, in our Flask plus htmx web app.