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