Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Template Tags

00:00 In the previous lesson, I introduced you to rendering templates. In this lesson, I’ll be talking about conditional blocks and looping through them using template tags.

00:12 Tags are denoted by {% %}, and they’re kind of like keywords in Python. The first tag I’m going to show you is the conditional, built using {% if %}, {% endif %}, and an optional {% else %} clause. Let’s go construct a template using this tag.

00:33 I’ve modified this student message program from the previous lesson just a little bit. This time, the message will go to both students who did well and those who did poorly.

00:43 The first change is to add some extra student data. The second change is I’m loading a different template. Instead of good.txt, this time I’m loading all.txt.

00:57 Pretty much everything else is the same, so let’s go look at all.txt.

01:06 This template uses tags. This {% if %} tag does a comparison of the value for score against 80. If it evaluates to True, then the block between here and the {% else %} is what gets rendered.

01:20 If it evaluates to False, then the block between the {% else %} and the {% endif %} tag is what gets rendered instead.

01:28 It’s pretty much the template version of Python’s ifelse blocks. Let me just run this.

01:41 Here’s the result for Willow, same as before.

01:47 And here’s the note for Xander. Hopefully, he’ll do better next time.

01:56 Tags can also be used for looping, allowing you to reuse the section of template repeatedly. This program uses the same data as before, but instead of creating messages for the students, it creates a single CSV file exporting the data.

02:13 The template this time is called export.csv, and seeing as there’s only a single output file, this time its filename has been hard-coded. Instead of passing named values into the .render() function, here I’m using a context dictionary.

02:30 This pattern is quite common, and you’ll see it in a lot of templating libraries. In fact, when you get to rendering HTML in Flask, this is pretty much the only way to do it.

02:41 Like before, I open a file for writing. This time, I’m calling .render() with the context dictionary and writing it out all in one line. Let’s look at the export.csv template.

02:55 That’s a bit hard to read, isn’t it? Unfortunately, it has to be. The whitespace and the newlines in this file are important. If you put a space between the {% for %} tag and the student value, you’d get a space in the resulting export file. Likewise, as this is a CSV file, all the values have to be on the same line with the new line at the end.

03:19 What you put in the template forms the actual output. If you had put the {% endfor %} tag on the same line as above, your resulting file would all be in a single line. Later, when I show you HTML, none of this will matter because HTML ignores spaces.

03:35 But for a CSV file, this is important. Sneakily, I’ve run the program in the background. Let’s look at the results. And there you go, a CSV export of the student’s marks.

03:53 Next up, a quick intro to the Flask web application framework.

Become a Member to join the conversation.