Flask Series: Templating

Friday, May 8, 2015 08:49 · 464 words · 3 minutes read

Flask Series

  1. Prepare the Environment
  2. Structure the Application
  3. Application Configuration
  4. Templating
  5. Model
  6. Testing
  7. Views and Web Forms
  8. Error Management
  9. Security
  10. Optimizations
  11. Healthcheck and Monitoring
  12. Internationalization
  13. Deployment

Templating allows data representation in different way, it combines template and data. The template is a document with placeholders, where the actual data will be used, when the template is processed by the template engine. The template can contain control structures like for loops, if statements, etc.

By default Flask uses Jinja2 as its template engine. Jinja2 allows developers to produce different output result file based on simple template text file. In this blog post I will cover how to use:

  • rendering templates
  • variables
  • comments
  • control structures: for loops and if statements
  • filters
  • template inheritance and blocks

Rendering Templates

You should use the render_template method to achieve this goal, where you have to specify the template name and you could pass some data as keyword arguments:

Variables

Variables are passed to the template via the context dictionary. In Jinja2 the double curly braces are used as a print statement. So if you want to print the value of the title variable, you should use the following code snippet:

Should you want to access variable’s attributes you have two approaches to achieve that goal:

or

In case of non-existent variable or attribute, an undefined value will be returned.

Comments

To be able to provide a comment in your template file or just to comment out a code block you should surround it with

or

Control Structures: for loop

for control structure allows loop over a sequence or dictionary of items.

Sequence:

Control Structures: if

if statement is used for branching a logic internally in the template:

Multiple branches can be used in the following way:

Filters

Filters allows developers to modify the value of the variable, they are separated by the variable with the pipe symbol ‘|’.

Filters can be chained:

Custom filters can be implemented and used in the templates:

And now you could use it in the following way:

Template Inheritance and Blocks

Template inheritance allows developers to prepare a common layout for the application and to define blocks that could be overridden in the child templates. In the Flask Bookshelf application I have implemented a layout.htm, that contains the skeleton of the web application:

The child templates inherit the base one (layout.htm) and provide their specifics using the defined blocks:

Below I will describe how to structure your Flask application to enable the Jinja2 template engine and use it internally.

Added a templates folder under bookshelf/main

Added a templates folder under bookshelf/admin

In the next blog post I will describe how to model your data in your Flask project and use database systems.

The complete demo application, described in this blog post, can be found here.
comments powered by Disqus

Buy Me A Coffee