Tornado-05, tornado template

Time:2022-11-24

Tornado template

1. Basic understanding of templates

Preparation

Tornado itself provides a lightweight, fast and flexible templating language in the tornado.template module.

A template is a web page with a fixed format: the format is fixed, but the content changes. A template is an HTML file that allows Python code snippets to be embedded

Create a new lesson4 folder under the tornado folder Create a new start1.py file under this folder Add some basic content first
Then follow these steps
1. Add the template_path configuration in the parameters of Application, specify the path of the template and the path of the static file

template_path='templates',
static_path='static',

2. Create a new template folder under the lesson4 folder (used to store html files)
Create a new static folder under the lesson4 folder (used to store pictures, css, js and other static files)

3. Use the render method to render the template and add the following code

(r'/', IndexHandler), # add route

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index.html')

This code tells Tornado to find a file named index.html in the templates folder, read the contents, and send it to the browser

4. Create a new index.html file under the templates folder
Add the following code to the body

hello boys and girls!

The result displayed after running


Tornado-05, tornado template

2. Symbols in templates

{{ }} The expression is surrounded by double braces, the content can be any python expression, and the words in the double braces are placeholders
{% %} Template control statements surrounded by {% %}
{# #} template comment format

Place variable names or expressions in double braces, and the variable names must be the same as the incoming keyword parameter names
Create a new IndexHandler.html in templates
And add the following code in the body of this file

<form method="post" action="/html">
    <p>username<br><input type="text" name="name"></p>
    <p>password<br><input type="text" name="password"></p>
    <input type="submit">
</form>

Create a new index2.html file in templates
And add the following code in the body

<b>User {{ username }} is welcome to log in</b>

processor file

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        self.render('index1.html')

    def post(self):
        username = self.get_argument('name','no')
        self.render('index2.html',username=username)


Tornado-05, tornado template

Tornado-05, tornado template

Tornado’s template processing is very convenient, and any python expression can be passed in {{}}:

#For the convenience of testing, create a new simple route
(r'/temp',TempHandler),
class TempHandler(tornado.web.RequestHandler):
    def get(self):
        username = self.get_argument('name','no')
        self.render('temp.html',username=username)

Next we add the following code in the index.html file

hello boys and girls!<br>
<! Pass in an expression -->
{{ 10+27 }}
<br>
{{ time.time() }} <! -- Can execute a function -->
<br>
{{ haha() }} <! Custom functions can also be passed in -->

Add the following code in TempHandler in start1.py

def haha(self):
        return 'here is tornado'

    def get(self):
        username = self.get_argument('name','no')
        import time
        li = ['a','b','c','d']
        self.render('index.html',
                    username=username,
                    time=time,
                    haha=self.haha,
                    li=li
                    )

Tornado-05, tornado template

3. Control Statements

Most of the control statements work just like the corresponding Python statements

The return of the control statement ends with the nearest {% end %}

if condition judgment

{% if condition %}
     ...
{% elif condition %}
     ...
{% else %}
     ...
{% end %}

example:

{% if username!='no' %}
    User {{ username }} is welcome to log in
{% else %}
    You are not logged in
{% end %}

for loop

{% for var in expr %} # Note that the control statements in the template do not need to add colons
     ...
{% end %}

How to write in html file

<br>
{% for i in li%}
    {{ i }} <br>
{% end %}


The above is the basic knowledge of the template, and we will learn more about it later.