tornado basic template frame base 04-

 

01 Templates

Template demo

Configuration path

Configuration path for templates and static files in the application:

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

template

<body> Welcome log {{username}} </ body>

Handler

class TemplatesHandler(tornado.web.RequestHandler):

    def get(self):

  self.write('templates')

  self.render('01in_out.html')

    def post(self, *args, **kwargs):

        user = self.get_argument('name', 'no')

        self.render('02templates.html',

                    username=user

                    ) # The argument to the template file.

In the tornado, the template is a html file, but a template through the template syntax, after the service rendered by the tornado, can dynamically populate data into the page

tornado comes template syntax, not necessary to use other templates plugins

Template concept

Must master: the template is the html file, but added the template syntax, you need to render the server to display data properly

02 template symbol

{{expression}}

{{Expression}} using any intermediate python expression, or a variable

Example: {1} + {1}

The time.time {{()}} # import time the handler to the page, and to pass the time parameter in the handler.

{%  directives %}

Other templates command

Examples

{% if 1 %}     this is if {% end %}

{#  … #}

In the template for comments python expressions run, you need to use this template syntax

Examples

{# time.time() #}}

{{!   {%!   {#!

Cancel the execution, if you do not execute the content, need to print out the template symbol on the page, only need to add an exclamation point (!) To

Examples

{{! 1 + 1}}

{%! if 1 %} this is if {%! end %}

{#! time.time() #}}

Define a variable to use in the template set

{% set a = 0 %}

Change of variables have to use set

{% set a += 1 %}

Control statements

if the judge

Can be used to determine if the template

Note: Finally at the end of {% end%}

Examples

{% if username != 'no' %}    

Welcome {{username}} {% else%} Log in    

please sign in

{% end %}

loop statement

for loop

You can use the template for loop in tornado

Note: Finally at the end of {% end%}

Examples

{% for i in urllist %}

    {{ i }}

{% end %}

while loop

You can use a while loop in tornado template

Note: Finally at the end of {% end%}

Examples

{% set a = 0 %}

{% while a<5 %}

    {{ a }}     {% set a += 1 %}

{% end %}

Template syntax

{{

This symbol is placed arbitrary python expressions, or variables in the template

{%

Template command into this symbol, such as if, for and while, etc.

It should be noted that if the use of commands that need to add {% end%}

除此之外,异常处理 try 也可以在模板中使用,但是这样做会让模板变得像 python 模块一样,因此并不建议大家这么做

{#

模板中的注释语句,可以让模板中指令不执行

03 模板转义

参数

atga = "百度" #里面是百度的网址,这里显示的”百度“

模板

{{ atga }}

转义

页面并没有解析,只是当作一个字符串,直接在页面上打印出来

tornado默认是自动的转义,传入的数据都会当作字符串,不会被浏览器解析

模板去掉转义

局部去掉转义

{% raw atga %}

raw

raw 可以自模板中去掉转义,让 tornado 在渲染的时候不去转义变量

模板去掉转义

模板去转义/全局取消转义,相当于全部加raw

{% autoescape None %}

autoescape

在模板中添加上面代码之后,当前模板不再转义

escape

{{ escape(atga) }}

在开启模板不转义之后,可以使用 escape 来添加转义

全局去掉转义

在 Application 中添加如下配置:

autoescape=None,

autoescape

去掉整个项目的转义,配置之后,整个项目中的模板不再转义

<!-- -->前端注释的内容不显示但是还是会执行的.

04 静态文件引用

Application

static_path='static',

两种引入方式

 

 

 

路由配置

添加此配置之后,tornado就能自己找到静态文件

static/

 

自动查找

在 Tornado 模板中,static 是个关键词,能够自动替换成 static_path 后的内容

static_url

 

添加版本号

使用此方法时,Tornado 会自动地给静态文件添加版本号,第2次请求的时候,如果版本号更改了,浏览器会自动的缓存新的静态文件,

 

html里面的注释写法:

<!-- abc -->在浏览器不显示但是执行.

 

Guess you like

Origin www.cnblogs.com/winfun/p/10972482.html