Python - flask template rendering Jinja2

Python-flask template rendering Jinja2

A template is a text file containing the response, which comprises a dynamic part represented by the placeholder variable, which value is specific only to know the context of the request. Replace the variable real value, then returns the final response string obtained, this process is called rendering. In order to render the template, Flask use a powerful template engine called the Jinja2.

Introduction jinja2

jinja2 Flask is a template system developed by the authors, it was originally a template engine imitation django template, templates provide support for the Flask, because of its flexible, fast and safe, etc. are widely used.

jinja2 advantage

jinja2 reason is widely used because it has the following advantages:

  1. With respect to the Template, jinja2 more flexible, which provides control structure, expression and inheritance.
  2. Only with respect to the control structure Mako, jinja2, not allowed to write a lot of business logic in the template.
  3. With respect to the Django template, jinja2 better performance.
  4. Readability great Jinja2 template.

jinja2 grammar

As a template system, it also provides special syntax, then we were prepared in accordance with its support for syntax, you can use jinja2 module for rendering.

In jinja2, there are three syntax:

  1. %} {% Control structure
  2. Variable values ​​{}
  3. Note {# #}

jinja2 variable filter

Filter Name Explanation
safe Not escaped when rendering value
capitalize To convert the value into the first letter
lower The escape values ​​lowercase
upper The value is converted to uppercase
title The value of the first letter of each word are converted to uppercase
trim The value of the trailing spaces removed
striptags Before rendering the value of all the HTML tags are deleted

Filters can be used to modify variables, filters were added after the variable name, using the intermediate vertical partition. For example, the following template to display the value of name capitalized form:

    Hello, {{ name|capitalize }}

jinja2 control structure

Jinja2 offers a variety of control structures, it can be used to alter the rendering process template. This section describes an example of using a simple control structure in which the most useful.
The following example shows how to use conditional control statements in the template:

	{% if user %}  
    Hello, {{ user }}! {% else %}
    Hello, Stranger! {% endif %}

Another common requirement is to render a set of elements in the template. The following example shows how to use a for loop to achieve this requirement:

<ul>
     {% for comment in comments %}
            <li>{{ comment }}</li>
     {% endfor %} 
</ul>

Jinja2 also supports macros. Function macro is similar Python code. E.g:

{% macro render_comment(comment) %}
     <li>{{ comment }}</li>
{% endmacro %} 
 
<ul>
     {% for comment in comments %}
          {{ render_comment(comment) }}
     {% endfor %}
</ul>

In order to re-use the macro, we can save it in a separate file, and then import the template you want to use in:

{% import 'macros.html' as macros %}
<ul>
      {% for comment in comments %}
            {{ macros.render_comment(comment) }}
      {% endfor %}
</ul> 

Need to be written in the template code segment reuse multiple individual files, and then included in all templates, to avoid duplication:

{% include 'common.html' %}

Another powerful way to reuse code is template inheritance, which is similar to Python code in the class inheritance. First, create a base template named base.html of:

<html> 
<head>
     {% block head %}
     <title>{% block title %}{% endblock %} - My Application</title> 
     {% endblock %}
</head>
 <body> 
      {% block body %}
      {% endblock %}
</body> 
</html>

block tag defined elements derived template can be modified. In this example, we define a called head, title and body of the block. Note, title contained in the head. The following example is a group derived template template:

{% extends "base.html" %} 
{% block title %}Index{% endblock %} 
{% block head %}
     {{ super() }}
     <style>  
     </style> 
{% endblock %}
{% block body %} 
<h1>Hello, World!</h1> 
{% endblock %}

This declaration extends the instruction template derived from base.html. After the command extends the base in the template is re-defined three blocks, which will be inserted into the template engine place. Note that the new head block defined in the template group is not empty its content, the use of super () Gets the original content.

Published 33 original articles · won praise 1 · views 2292

Guess you like

Origin blog.csdn.net/qq_40805620/article/details/100677716