Building Data-Driven Web Apps with Flask and SQLAlchemy Transcripts
Chapter: Jinja2 templates
Lecture: Concept: Jinja2 templates
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's review this Jinja template HTML file as a concept. Here are the basics, we've got standard HTML and in here, we've added a couple of things.
0:11
We have the {%. And that indicates small bits of Python sprinkled in here. And then for each one of those we need a closing block.
0:19
So, we have a % for, and a % endfor. And we have a % if, so we have a % endif. The first thing that we wanted to do was loop over all packages.
0:30
How do you do that in Python? You say for thing in container. So we do the same thing here except for that we don't include the colon at the end.
0:38
The percent stuff does the block for us. And we have the {% on both ends. We also wanted to get some of the text out
0:46
and just drop that in the page as HTML. This is what it looks like as static data for the users viewing this page.
0:53
So, we want to put the project ID here. Which is going to be the name. We get to the database, the name is the ID.
1:00
So we don't have a separate field for that. So we say that by {{ expression here this can be either just a straight value
1:08
or as you saw you can even call functions here. Also, we want to have an if statement. So at the bottom we're going to say
1:13
only show this span with a summary if the summary is actually set. We didn't do this in our demo, but you get the idea right?
1:21
So, we can say we only want to show this bottom part by doing some Python evaluation on the % if. Pretty straight forward, right?
1:28
We run this with some set of data it will come out looking more or less like this. We have little hyperlinks, and then the summaries
1:35
that kind of trail off so we don't show you all the text. All right, so, pretty straight forward.
1:40
These are the main working elements of the Jinja templates.