模板继承和UImodul 和 UImethods


  • 模板的继承

    • {% extends path %}

      • path为templates下的相对路径
    • {% block block_name %}content{% end %}

      • block_name:块名字
      • content:中间包裹的内容
      • 和django的block一致
    • {% include path %}
      • 包含的模板路径
    • 参考链接:https://www.cnblogs.com/ivy-blogs/p/10654806.html
  • 函数跟类的导入:
  • UImodul和UImethods
    • 新建个util包
    • 在里面新建uimethods.py和uimodules.py两个文件
    • 在定义app的路由所在的文件下导入这两个文件
    • Application给ui_methods和ui_modules传参等于导入的文件名
       from tornado.web import RequestHandler, Application
      from tornado.httpserver import HTTPServer
      from tornado.options import options, define
      from tornado.ioloop import IOLoop
      from util import uimethods, uimodules define('port', default=7981, type=int) class IndexHandle(RequestHandler):
      def get(self):
      self.render('index.html') application = Application(
      handlers=[
      (r'/', IndexHandle)
      ],
      template_path='templates',
      ui_methods=uimethods,
      ui_modules=uimodules
      ) if __name__ == '__main__':
      options.parse_command_line()
      app = HTTPServer(application)
      app.listen(options.port)
      IOLoop.current().start()
    • ulmethods使用
      • def test(self): return 'test'
      • ulmethods里面的方法第一个参数必须为self
    • ulmodules使用
      •  from tornado.web import UIModule
        
         class TestModule(UIModule):
        def render(self, *args, **kwargs):
        return 'TestModule'

        先建的类必须继承自UIModule类,当该类实例化的时候,会自动执行render方法

      •  from tornado.web import UIModule
        
         class TestModule(UIModule):
        def render(self, *args, **kwargs):
        self.render_string('index.html')

        渲染页面

      •  from tornado.web import UIModule
        
         class TestModule(UIModule):
        def render(self, *args, **kwargs):
        self.render_string('index.html') def javascript_files(self):
        return 'your javascript_files path' def css_files(self):
        return 'your jcss_files path'

        静态资源加载,如果路径有多个,则返回列表形式

      •  <!DOCTYPE html>
        <html lang="en">
        <head>
        <meta charset="UTF-8">
        <title>Title</title>
        </head>
        <body>
        {% module TestModule() %}
        <br>
        {{ test() }}
        </body>
        </html>

        具体使用

  • 模板的其他命令
    •  def test(self): return 'test'
      
       def upper(self, text):
      if isinstance(text, str):
      return text.upper()
      raise TypeError

      在ulmethod里面定义一个upper方法

    •  <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>Title</title>
      </head>
      <body>
      {{ upper('hello') }}
      </body>
      </html>

      使用该方法

    • 当传入的参数比较大的时候,这种方法显得不太美观
       <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>Title</title>
      </head>
      <body>
      {% apply upper %}
      asdasdasddddddddd
      {% end %}
      </body>
      </html>

      apply可以扩大后面方法的作用范围,以end结尾

    •  <!DOCTYPE html>
      <html lang="en">
      <head>
      <meta charset="UTF-8">
      <title>Title</title>
      </head>
      <body>
      {{ linkify ('http://www.baidu.com') }}
      {% raw linkify ('http://www.baidu.com') %}
      </body>
      </html>

      linkify后接一个元祖,会自动识别元祖内的url,将其解析为a标签,但需要取消转译

最新文章

  1. 20155315庄艺霖--对做中学的理解及对c语言和Java的看法
  2. [GodLove]Wine93 Tarining Round #9
  3. BZOJ4383 : [POI2015]Pustynia
  4. ios授权登录过程中一直提示“没有安装qq”
  5. 犯过错误的C语言问题
  6. UITextField的常用属性,Delegate,重绘
  7. 用UNetbootin来安装USB LINUX,好像比ULTRA ISO省事
  8. Intra Chroma Prediction
  9. Linux常用操作
  10. Python 装饰器示例
  11. 2018-2019-2 网络对抗技术 20162329 Exp4 恶意代码分析
  12. Navicat premium 12破解版
  13. $Django RESTful规范
  14. 图解JAVA参数传递
  15. 学习笔记TF017:自然语言处理、RNN、LSTM
  16. 第三篇、Python函数
  17. PAT L3-003 社交集群
  18. OpenSSH多路复用Multiplexing配置
  19. [六字真言]2.嘛.异常定制和通用.md
  20. [转]Java中使用Runtime和Process类运行外部程序

热门文章

  1. Object.keys()方法
  2. beforeEach 之 next
  3. 手写 Promise 符合 Promise/A+规范
  4. ajax上传文件,通过FromData把数据传给后端
  5. redis 持久化RDB、AOF
  6. nested exception is java.lang.StackOverflowError解析
  7. ASP.NET Core应用的7种依赖注入方式
  8. Head First设计模式——桥接模式
  9. Error while importing sbt project:--创建sbt项目导入文件出错
  10. CentOS7安装和配置ftp服务