Full Web Apps with FastAPI Transcripts
Chapter: Course conclusion and review
Lecture: Template languages
0:00 While returning the string "<h1>Hello web!</h1>" from our API method, our
0:05 view endpoint there was fun,
0:07 it is not at all the way we should be building web applications.
0:10 What we should be doing is getting a standalone,
0:13 dedicated HTML template language to generate dynamic HTML from the data our web app provides it,
0:20 and then turning that into proper HTML with all the cool stuff,
0:24 like global shared layout pages and little smaller,
0:28 customized, specialized pages that just fill in the holes about what's unique for each page
0:33 We also looked at three possible candidates to solve this problem.
0:37 Jinja2, Mako, and Chameleon. Now Jinja2 is sort of built-in,
0:43 sort of built-in to FastAPI.
0:44 It's not entirely built-in cause you've got to install Jinja2 in order to use it,
0:48 but there are some capabilities in there. Chameleon,
0:50 not at all, but we went through the different languages and we said,
0:54 well, what are the advantages and disadvantages of each?
0:57 And I said, I'm going to use Chameleon because I believe, at least for the
1:01 things that I value most, it is the best template language.
1:04 Remember, you don't get to write arbitrary Python,
1:07 which some people see as a disadvantage.
1:09 But I see that as a way to enforce you
1:12 to write better factored code and to put that code not in your template,
1:16 but somewhere better where it belongs.
1:17 Also, the templates are proper HTML, like this thing you see here. This is
1:23 valid HTML with, with some weird attributes that get ignored.
1:26 But that's OK for HTML, as opposed to Jinja,
1:28 which has all these begin end blocks and little funky segments that are not valid
1:32 HTML. So for that reason,
1:34 I said, hey, I think this is the one we're going to use.
1:35 We used it, we used my fastapi-chameleon decorator in order to
1:41 make this incredibly easy to use.
1:43 If you don't like Chameleon, again,
1:44 there's the fastapi-jinja template that is almost exactly the same thing,
1:49 but for Jinja2, so you can just use that one as well.
1:52 Those are really the two best choices,
1:53 I think. I definitely prefer Chameleon
1:55 so here we go and you can choose whichever one you like.