tornado template

时间:2023-12-02 20:08:08

若果使用Tornado进行web开发可能会用到模板功能,页面继承,嵌套... 多页应用模板的处理多半依赖后端(SPA就可以动态加载局部视图),就算是RESTfull的API设计,也不妨碍同时提供部分模板处理的接口。

http://www.tornadoweb.org/en/stable/template.html

http://www.cnblogs.com/liaofeifight/p/4962216.html

http://www.cnblogs.com/inevermore/p/4190382.html

http://www.cnblogs.com/liaofeifight/p/4968986.html

基本语法

Syntax Reference

逻辑控制: {% for a in list%}

表达式: {{expression}}

显示简单模板

    def get(self):
# return JsonResponse(self, '50000')
person = {'name': 'CooMark', 'age': 30} self.render('index2.html', **person)
<html>

<head>
<meta charset="UTF-8">
<title>test </title>
</head> <body>
<h3>Tornado template render function - {{name}} - age {{age}}</h3>
</body> </html>

模板嵌套

这个要使用模板指令语法{% include filename %}, render部分的不变

header.html

<h1>This is header</h1>

index2.thml

<html>

<head>
<meta charset="UTF-8">
<title>test </title>
</head> <body>
{%include header.html%}
<hr>
<h3>Tornado template render function - {{name}} - age {{age}}</h3>
</body> </html>

效果图

tornado template