web框架--tornado框架之模板引擎
使用Tornado
实现一个简陋的任务表功能demo来讲解tornado框架模板引擎
一、demo目录结构
二、具体文件内容
2.1、commons.css
.body{
margin: 0;
background-color: bisque;
}
2.2、index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- <link rel="stylesheet" href="../static/commons.css"/>-->
<link rel="stylesheet" href='{{static_url("commons.css")}}'/>
</head>
<body>
<h1>{{ag}}</h1>
<h2>{{test_uimethod()}}</h2>
<h3>{% module MyClass() %}</h3>
<form method="post">
<input type="text" name="name"/>
<input type="submit" value="提交"/> </form> <h1>显示内容</h1>
<ul>
{% for item in contents %}
<li>{{item}}</li>
{% end %}
</ul>
</body>
</html>
2.3、uimodule.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tornado.web import UIModule class MyClass(UIModule):
def render(self, *args, **kwargs):
return 'UIModule'
2.4、uimethod.py
#!/usr/bin/env python
# -*- coding: utf-8 -*- def test_uimethod(self):
return 'uimethod'
2.5、index.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tornado.web
import tornado.ioloop
import uimethod as ut
import uimodule as ud class IndexHandle(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
self.render('index.html', contents=CONTENTS_LIST, ag="") def post(self, *args, **kwargs):
CONTENTS_LIST.append(self.get_argument('name'))
self.render('index.html', contents=CONTENTS_LIST, ag='this is ag') if __name__ == '__main__':
CONTENTS_LIST = [] #为存放的是输入框输入的内容
#字典表示的是配置文件
settings = {
'template_path': 'template', #模板文件的存放位置
'static_path': 'static', #静态文件的存放位置
'static_url_prefix': 'static/', #静态文件前缀,减少每个文件引入都要加前缀的麻烦
'ui_methods': ut,
'ui_modules': ud,
} application = tornado.web.Application([
(r'/index', IndexHandle)
], **settings)
application.listen(80) #设置服务端的监听端口
tornado.ioloop.IOLoop.instance().start() #阻塞服务端进程, 等待客户端的访问
2.6、demo运行的效果图
demo详解:
- 模板引擎中的
{{key}}
表示取key
对应的值, 当key
为函数时候执行该函数并取该函数结果. 例如index.html
文件中的<h1>{{ag}}</h1>
实际上取得是index.py
的self.render("index.html", ag="this is ag", contents=CONTENTS_LIST)
中的参数ag
的值 <h1>{{test_uimethod()}}</h1>
这里执行的是自定义函数, 我们将这个自定义函数写在uimethod.py
文件中, 并且在index.py
文件中导入, 然后将index.py
文件中的settings
配置增加一行'ui_methods': ut
, 该行内容表示模板引擎可执行自定义函数- 模板引擎中的
{%%}
可用于循环语句和条件语言以及自定义类的执行,{% for item in contents %}
此处正是用于循环遍历contents
中的内容 <h1>{%module MyClass()%}</h1>
此处表示模板引擎执行自定义类, 该类的文件对应的是uimodule.py
文件, 我们需要在index.py
的settings
中增加一行'ui_modules': ud
, 改行表示模板引擎可使用自定义类- 注意, 我们将
index.html
文件引入css
的方式改为了<link rel="stylesheet" href='{{static_url("commons.css")}}'>
,static_url()
是模板引擎内置的自定义函数, 用该函数引入css
文件时候, 仅当css
文件内容发生变化时候, 浏览器才会重新缓存该css
文件
web框架--tornado框架之模板引擎的更多相关文章
- web框架--tornado框架之模板引擎继承
使用模板的继承可以重复使用相同结构的模板, 可以大大减少代码量 入门实例 一.demo目录结构 注解: master.html为模板内容,被index.html,account.html引用 二.各文 ...
- Tornado框架配置使用Jinja2模板引擎
安装jinja2包 pip install jinja2 定义继承tornado.web.RequestHandler的子类BaseHandler.如果请求处理类继承这个类将会使用jinja模板引擎: ...
- flask框架下的jinja2模板引擎(1)(模板渲染)
#转载请留言联系 模板是什么? 在 flask 框架中,视图函数有两个作用:处理业务逻辑和返回响应内容.在大型应用中,把业务逻辑和表现内容放在一起,会增加代码的复杂度和维护成本.模板作用即是承担视图函 ...
- flask框架下的jinja2模板引擎(3)(模板继承与可以在模板使用的变量、方法)
flask 框架下的jinja2模块引擎(1):https://www.cnblogs.com/chichung/p/9774556.html flask 框架下的jinja2模块引擎(2):http ...
- laravel框架总结(二) -- blade模板引擎
## 1.基本用法 ##情形1 $name = laravel5 <div class="title"> {{$name}} {{$name}}</div> ...
- flask框架下的jinja2模板引擎(2)(过滤器与自定义过滤器)
flask框架下的jinja2模块引擎(1):https://www.cnblogs.com/chichung/p/9774556.html 这篇论文主要用来记录下 jinja2 的过滤器. 什么是过 ...
- Spring Boot 系列(五)web开发-Thymeleaf、FreeMarker模板引擎
前面几篇介绍了返回json数据提供良好的RESTful api,下面我们介绍如何把处理完的数据渲染到页面上. Spring Boot 使用模板引擎 Spring Boot 推荐使用Thymeleaf. ...
- web框架--tornado框架之初识
在python中常见的web框架构建模式有以下两种: *MVC框架: * 数据库相关操作的Models 视图文件的Views 业务逻辑的Controllers MTV框架: 数据库相关操作的Model ...
- [SpringBoot——Web开发(使用Thymeleaf模板引擎)]
[文字只能描述片段信息,具体细节参考代码] https://github.com/HCJ-shadow/SpringBootPlus 引入POM依赖 <properties> <ja ...
随机推荐
- CF1207F Koala and Notebook(BFS)
你可能会好奇为什么只有一个 BFS 的标签,却还能够排到 F 的位置. 因为它实在是太 简 单 了 有更新 首先,比较两个数,可以先比较两个数的长度,然后比较两个数看成数字串后的字典序. 不妨先把每条 ...
- LeetCode 283:移动零 Move Zeroes
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. Given an array nums, write a function to move all 0' ...
- redis延迟队列
异步消息队列 Redis 的 list(列表) 数据结构常用来作为异步消息队列使用,使用rpush/lpush操作入队列, 使用 lpop 和 rpop 来出队列. > rpush notify ...
- 时间复杂度o(1), o(n), o(logn), o(nlogn)
1.时间复杂度o(1), o(n), o(logn), o(nlogn).算法时间复杂度的时候有说o(1), o(n), o(logn), o(nlogn),这是算法的时空复杂度的表示.不仅仅用于表示 ...
- ElasticSearch简介(一)——基础
基本概念 1. Node 与 Cluster Elastic 本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以运行多个 Elastic 实例. 单个 Elastic 实例称为一个节点 ...
- WPF xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<Button Grid.Row="1" Content="Load Data" BorderBrush="Black" Border ...
- ASP.NET MVC 实现简单的登录
1.创建一个控制器 如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; ...
- Review: Basic Knowledge about WebForm
Asp.net shanzm
- identityServer3+ADFS实现域用户登录授权
准备: ADFS安装配置 https://www.cnblogs.com/luoyedemeng/articles/9837685.html 添加一个Providers private void Co ...
- Java引用类型原理深度剖析,看完文章,90%的人都收藏了
本文为synchronized系列第二篇.主要内容为分析偏向锁的实现. 偏向锁的诞生背景和基本原理在上文中已经讲过了. 本文将分为几块内容: 1.偏向锁的入口 2.偏向锁的获取流程 3.偏向锁的撤销流 ...