• 模板继承


     

    继承:
    模板继承使用extends标签实现。通过使用block来给子模板开放接口。
    1、extends必须是模板中的第一个出现的标签。
    2、子模板中的所有内容,必须出现在父模板定义好的block中,否则tornado将不会渲染。
    3、如果出现重复代码,就应该考虑使用模板。
    4、尽可能多的定义block,方便子模板实现更细的需求。
    5、如果在某个block中,要使用父模板的内容,使用block.super获取。

    
    

    {%extends ‘base.html’%}
    {%block title%}xxx{%endblock%}
    {%block content %}
    xxx
    {%end%}

    
    


    引用:
    include标签可以包含一个html模板到当前模板中。和继承不同,include是把html模板在此处展开。
    例如:
    {% include ‘head.html’%}


    ##05extend.py #coding:utf-8 import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web from tornado.options import define,options define('port',default=8000,help='run port',type=int) define('version',default='0.0.1',help='version 0.0.1',type=str) class ExtendHandler(tornado.web.RequestHandler): def get(self): username = self.get_argument('name', 'no') self.render('04base.html',username=username) class IndexHandler(tornado.web.RequestHandler): def get(self): username=self.get_argument('name','no') self.render('05extend.html', username=username, ) if __name__ == "__main__": tornado.options.parse_command_line() # print(options.port) app=tornado.web.Application( handlers=[ (r'/index',IndexHandler), (r'/extend',ExtendHandler), ], template_path='templates', static_path='static', debug=True, #autoescape=None, #关闭自动转义 全局的 ) #固定写法: http_server=tornado.httpserver.HTTPServer(app) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()

      

    ## 04base.html
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>{% block title %}Tornado{% end %}</title>
        <link rel="shortcut icon" href="{{ static_url('images/favicon.ico') }}" type="image/x-icon" />
    </head>
    <body>
        {% block body %}
        this is base
        {% end %}
    </body>
    </html>
    
    ## 05extend.html
    {% extends "./04base.html" %}
    
    {% block title %} {{ username }} {% end %}
    
    {% block body %}
        {% if username!='no' %}
    
        欢迎,{{username}}
    </br>
    <img src="/static/images/c.jpg"  width="250px"  height="250px">
        {% else  %}
        请登录 <br>
    
        {% include "./06include.html" %}
        {% end %}
    
    {% end %}
    
    ## 06include.html
    您还未登录
    View Code
  • 相关阅读:
    c#队列的实现
    c#队列的实现
    C# 自定义控件制作和使用实例(winform)
    常见的位运算
    Clock()函数简单使用(C库函数)
    Python全局变量的简单使用
    PyQt5+Caffe+Opencv搭建人脸识别登录界面
    python3+pyqt5+opencv3简单使用
    OpenCV实现人脸检测
    opencv 截图并保存
  • 原文地址:https://www.cnblogs.com/lajiao/p/7732941.html
Copyright © 2020-2023  润新知