Table of Contents
  my_uimodules.py
  header.html
  main.py
 页面代码
这是主页
这是Page2
Home Web Front-end HTML Tutorial tornado总结4-html模板使用2_html/css_WEB-ITnose

tornado总结4-html模板使用2_html/css_WEB-ITnose

Jun 24, 2016 am 11:18 AM

参考地址

http://www.tornadoweb.org/en/stable/guide/templates.html?highlight=render#ui-modules


目的说明

  osc的所有页面顶部都有这个导航条, 我们可以使用tornado的UI modules来实现导航条,实现代码的复用.


代码结构

增加了一个my_uimodules.py,里面包含了供tornado模板使用的uimodules,增加了一个header.html里面是导航条的html代码,增加了一个page2.py,返回简单的html页面page2.html.


实际运行效果

访问路径 "/"时的效果

点击导航链接  "页面1"的效果

点击导航链接  "页面2"的效果


代码说明

  my_uimodules.py

import tornado.webclass HeaderBar(tornado.web.UIModule):    def render(self, location):        return self.render_string("header.html", location=location)class HeaderBar2(tornado.web.UIModule):    def render(self, location):        return self.render_string("header.html", location=location)
Copy after login

所有的模板驱动都继承自 tornado.web.UIModule , 然后调用render_string调用指定的模板和参数. 你可以在此处写一个顶部导航栏再写个侧边导航栏. 我只在这里写了一个顶部导航模板驱动. 它接受一个参数location, 我把它作为导航栏最右侧的页面说明. 本模板驱动返回的是header.html,并把location传给了它.

header.html

<div>    <a href="/">主页</a>    <a href="/page1">页面1</a>    <a href="/page2">页面2</a>    <span>你现在处于页面 {{ location }}</span></div>
Copy after login

本模板文件作为页面的顶部导航栏,并通过location参数得到当前的页面地址

main.py

import osimport tornado.httpserverimport tornado.ioloopimport tornado.webimport my_uimodulesfrom handlers.home import HomeHandlerfrom handlers.page1 import Page1Handlerfrom handlers.page2 import Page2Handlerclass PageNotFoundHandler(tornado.web.RequestHandler):    def get(self):        return self.write_error(404)class Application(tornado.web.Application):    def __init__(self):        handlers = [            (r"/", tornado.web.RedirectHandler, {"url": "/home"}),            (r"/home", HomeHandler),            (r"/page1", Page1Handler),            (r"/page2", Page2Handler),            (r".*", PageNotFoundHandler),        ]        settings = dict(            static_path= os.path.join(os.path.dirname(__file__), "static"),            template_path=os.path.join(os.path.dirname(__file__), "templates"),            ui_modules=my_uimodules,        )        tornado.web.Application.__init__(self, handlers, **settings)if __name__ == "__main__":    port = 8899    application = Application()    http_server = tornado.httpserver.HTTPServer(application, xheaders=True)    http_server.listen(port)    print('Listen on http://localhost:{0}'.format(port))    print('Listen on http://localhost:{0}/page1'.format(port))    print('Listen on http://localhost:{0}/page2'.format(port))    tornado.ioloop.IOLoop.instance().start()
Copy after login

在torndao进行初始化设置时, 在settings 里面指定了自定义模板所在模块.

"ui_modules=my_uimodules"

当tornado在处理模板时,如果碰到如下的语句, 它就会去刚才设置的ui_modules模块中去找 xxxx模板驱动

{% module xxxx %}
Copy after login

页面代码

home.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>主页</title></head><body>    {% module HeaderBar("主页") %}    <h1 id="这是主页">这是主页</h1>    <script src="/static/js/home.js"></script></body></html>
Copy after login

page1.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>{{ argu1 }}</title></head><body>    {% module HeaderBar("页面1") %}     <h1 id="这是Page">这是Page1</h1>     <ul>       {% for i in argu2 %}         <li>{{ i }}</li>       {% end %}     </ul></body></html>
Copy after login

page2.html

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Page2</title></head><body>    {% module HeaderBar("页面2") %}     <h1 id="这是Page">这是Page2</h1></body></html>
Copy after login

  

所有页面模板的body的第一行都添加了一个 {% module HeaderBar("页面2") %} ,如前面所讲当tornado在处理render的模板时如果碰到了这种格式的请求,就会去 ui_modules 指定的模块my_uimodules下面查找HeaderBar,并把"页面2"作为第一个参数传给了它, HeaderBar把这个参数当做location传给了 header.html.  

    就这样每个页面顶部导航栏的相同和不同之处都体现了出来.

  


代码git地址  

http://git.oschina.net/donggen/tornado-test







Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

See all articles