Python Friday #34: Jinja Templates

A web application that only returns plain strings is boring. To turn your Flask application into something optical pleasing, we need a template engine like Jinja.

This post is part of my journey to learn Python. You can find the other parts of this series here.

 

Install Jinja

Jinja is a modern and designer-friendly templating language for Python. Flask depends on Jinja and is therefore the obvious choice for a template engine. Luckily for us, we need to learn only a few tags to use it.

You can install Jinja with this command in a virtual environment:

 

Reuse your HTML and put the logic away from the view

The main benefit for Jinja, at least in my opinion, is that we can reuse our HTML. We only need to add a few tags to turn a static site into something dynamic. This simple behaviour is only possible if we keep the complexity and the whole logic outside of our HTML page.

 

Where to put your views?

Flask has a few principles and conventions, that simplify your developer life. One of those conventions is that you put your templates into a folder called template. Create a folder called template next to the file that contains your Flask application:

 

Render your template

In my minimalistic example from the first steps with Flask post, my view function index() returned a string. To use a template instead of a string, we need to call the function render_template(). The first argument is the name of the template (relative to the template folder), while everything else that we want to use in our view goes in this function call as named parameters:

The get_latest_tasks() function helps me to create a bit more complex data, that I will use later on.

 

Access variables in your HTML template

We can access the variables we used in our call to the render_template() function by putting them inside {{ }}. To access name, we can write {{ name }} and Jinja will replace this snipped with the value of that variable:

The browser renders Hello Johnny

 

For loops

We often want to iterate through a list of items. We need to tell our template where the for loop starts (line 3) and where it ends (line 5). Inside those two blocks we can access the data as we would in any other Python for loop:

Our tasks are displayed as a list with bulled points

 

If statements

Even when we put all our logic away from the view, a few if statements will stay. With an if statement we can show or hide blocks. As with the for loop, we need to tell where our if starts and where it ends:

If name exists and has a value, it will be shown.

We can extend this if statement to include an else branch:

It is even possible to use the full Python syntax for if blocks and add as many elif as you like. However, I suggest you try not to use everything that is possible and aim for a maintainable application.

 

Everything put together

When we put all the concepts of this post together, we get this HTML template:

The view function for /task is unchanged:

The rendered template looks a lot better in our browser that just a string:

The browser shows a list of all tasks and a matching icon depending on the state of done

With these parts you can go a long way. There is a lot more to Jinja than that, and if you hit a problem or want to know more, there is an extensive official documentation with all the details.

 

Next

With these parts you can write dynamic HTML pages. However, if you have multiple pages it quickly gets annoying when you have to change the layout. In the next post I explain how you can reuse your layout and only keep the specific parts of a site in your template.

4 thoughts on “Python Friday #34: Jinja Templates”

  1. Thank you for sharing these resources. Read through some of your other Python Friday articles. I am definitely including these in my team training reading recommendations.

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.