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 ...
随机推荐
- Codeforces Round #597 (Div. 2) C. Constanze's Machine dp
C. Constanze's Machine Constanze is the smartest girl in her village but she has bad eyesight. One d ...
- Chrome 手动安装.crx插件
在网上自己下载.crx的离线插件文件.或者通过朋友打包.crx文件.打包方式可参照Chrome 打包扩展程序 方法一: 打开Chrome浏览器,地址栏输入 chrome://extensions/ 将 ...
- ansible碎碎念
1. Using a SSH password instead of a key is not possible because Host Key checking is enabled and ss ...
- 基于OceanStor Dorado V3存储之精简高效 Smart 系列特性
基于OceanStor Dorado V3存储之精简高效 Smart 系列特性 1.1 在线重删 1.2 在线压缩 1.3 智能精简配置 1.4 智能服务质量控制 1.5 异构虚拟化 1.6 ...
- 2019-11-29-dotnet-core-使用-GBK-编码
原文:2019-11-29-dotnet-core-使用-GBK-编码 title author date CreateTime categories dotnet core 使用 GBK 编码 li ...
- Java生鲜电商平台-电商促销业务分析设计与系统架构
Java生鲜电商平台-电商促销业务分析设计与系统架构 说明:Java开源生鲜电商平台-电商促销业务分析设计与系统架构,列举的是常见的促销场景与源代码下载 左侧为享受促销的资格,常见为这三种: 首单 大 ...
- 工作笔记--adb命令篇
1.抓log方法 (bat文件) mkdir D:\logcatset /p miaoshu=请描述操作:adb logcat -v threadtime > D:\logcat\%miaosh ...
- logstash 入门篇
场景介绍 基于分布式集群海量日志数据,且分布在不同的服务器上,日志的采集以及可视化是需要我们解决的问题.ELK就是这么一个方案,当然我们这里主要讲解logstash安装配置和基础语法. ELK帮我们解 ...
- maven 学习---使用Maven构建项目
要构建一个基于Maven的项目,打开控制台,进入到 pom.xml 文件所放的项目文件夹,并发出以下命令: mvn package 这将执行Maven的“package”阶段. Maven构建生命周期 ...
- fatal error: openssl/bn.h: No such file or directory
出现如下错误 fatal error: openssl/bn.h: No such file or directory 解决办法 # sudo apt-get install libssl-dev