• tornado之模板语法


    专栏目录请点击

    前提

    我们新建如下目录结构
    在这里插入图片描述
    其中

    index.html

    DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Documenttitle>
    head>
    <body>
        <h2>Hello Worldh2>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    app.py

    import tornado.ioloop
    import tornado.web
    import os
    
    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            self.render("index.html")
    
    
    def make_app():
        return tornado.web.Application(
            # 路由
            [
                (r"/", MainHandler),
            ],
            # 网页模板
            template_path=os.path.join(os.path.dirname(__file__), "templates")
        )
    
    if __name__ == "__main__":
        app = make_app()
        app.listen(8888)
        tornado.ioloop.IOLoop.current().start()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    运行并访问http://localhost:8888/,他会返回如下结果在这里插入图片描述

    变量与表达式

    我们可以想模板index.html中传递一个变量和表达式
    index.html

    <body>
        <h2>Hello Worldh2>
        <h3>num:{{num}}h3>
        <h3>num + 10:{{num + 10}}h3>
        <h3>姓名1:{{person["name"]}}h3>
        <h3>年龄1:{{person["age"]}}h3>
        <h3>姓名2:{{name}}h3>
        <h3>年龄2:{{age}}h3>
        
        {% set sex = "男" %}
        <h3>性别:{{sex}}h3>
    body>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    handler

    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            num = 5
            person1 = {
                "name": "sunwukong",
                "age": 18
            }
            person2 = {
                "name": "zhubajie",
                "age": 25
            }
            self.render("index.html", num=num, person=person1, **person2)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    效果
    在这里插入图片描述

    流程控制

    循环

    index.html

    <ul>
        {% for _i in _list %}
        <li>{{_i}}li>
        {% end %}
    ul>
    <ul>
        {% for _index,_element in enumerate(_list) %}
        <li>序号:{{_index}},元素:{{_element}}li>
        {% end %}
    ul>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    handler

    _list = [1, 2, 3, 4]
    self.render("index.html",  _list=_list)
    
    • 1
    • 2

    此外,我们还可以使用while来做循环,不常用,这里就不做解释了
    效果
    在这里插入图片描述

    判断

    index.html

    <h2>ifh2>
    {% for _index,_element in enumerate(_list) %}
        {% if _element == 3 %}
        <li>输出的元素为3,序号:{{_index}},元素:{{_element}}li>
        {% end %}
    {% end %}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    handler

    _list = [1, 2, 3, 4]
    self.render("index.html",  _list=_list)
    
    • 1
    • 2

    效果
    在这里插入图片描述

    函数

    static_url

    这是一个内置函数,用于用于静态路径的引入

    我们在项目的根目录下新建static/css/index.css,并写上如下代码

    index.css

    .title{
        color: red;
    }
    
    • 1
    • 2
    • 3

    index.html

    <body>
        <h2>Hello Worldh2>
        <p class="title">我爱我的祖国p>
    body>
    
    • 1
    • 2
    • 3
    • 4

    handler

    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            self.render("index.html")
    
    
    def make_app():
        return tornado.web.Application(
            # 路由
            [
                (r"/", MainHandler),
            ],
            # 网页模板
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            # 静态路径
            static_path=os.path.join(os.path.dirname(__file__),"static")
        )
    
    
    if __name__ == "__main__":
        app = make_app()
        app.listen(8888)
        tornado.ioloop.IOLoop.current().start()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    效果
    在这里插入图片描述

    自定义函数

    index.html

    <body>
        <h2>Hello Worldh2>
        <section>
            <span>resultspan>
            <span>{{myFun(1,2)}}span>
        section>
    body>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    handler

    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            def myFun(num1,num2):
                return num1 + num2
            self.render("index.html", myFun=myFun)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    结果

    在这里插入图片描述

  • 相关阅读:
    解决尚医通com.aliyun.oss 和com.aliyun 爆红
    Nginx
    量子点表面修饰PEG/抗体/金属/细胞膜/无机材料标记与制备
    SpringMVC程序开发
    EasyExcel文件导出,出现有文件但没有数据的问题
    终于读完了阿里云p9专家分享云原生Kubernetes全栈架构师实战文档
    Git Commit Message规范
    【Unity入门计划】基本概念(6)-精灵渲染器 Sprite Renderer
    CRM助力客户开发
    Docker最新超详细教程——Docker创建运行Redis并挂载
  • 原文地址:https://blog.csdn.net/youhebuke225/article/details/126264643