模板基礎

模板:html

在tornado中,模板就是一個html文件,可是模板經過模板語法,由tornado服務渲染以後,能夠動態的往頁面中填入數據 。tornado自帶模板語法,不須要再用其餘的模板插件python

模板初識:web

以前咱們經過render能夠返回一個html頁面,不過那都是固定的頁面,固定的數據,可是若是數據是不肯定的,是會不斷改變的,該怎麼作呢?express

是否能夠先把頁面寫好,而後預留出固定的位置,在須要的時候再填入數據便可?瀏覽器

04-templates.py文件:app

import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options
from tornado.web import RequestHandler
from tornado.options import define,options

define('port',default=8080,help='run server',type=int)


class MainHandler(RequestHandler):
    def get(self):
        self.render('in_out.html')
    def post(self):
        name = self.get_argument('name','')
        self.write(name)


class TemHandler(RequestHandler):
    def get(self):
        self.write('hello world')
    def post(self):
        name = self.get_argument('name','')
        self.render('02-templates.html',username= name)

application = tornado.web.Application(
    handlers=[
        (r'/',MainHandler),
        (r'/tem',TemHandler),
    ],
    debug=True,
    template_path = 'templates',
    static_path='static'
)
if __name__ == '__main__':
    tornado.options.parse_command_line()
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.current().start()

請求的html表單,in_out.html:tornado

<body>
    <form method="post" action="/tem">
        <p>用戶名<input type="text" name="name"></p>
        <p>密碼<input type="password" name="password"></p>
        <input type="submit">
    </form>
</body>

返回的html模板,02-templates.htmloop

<body>
    歡迎 {{ username }} 登錄
</body>

 

模板語法,模板符號:post

{{ expression }}google

用 {{ expression }} 中間是任何 python 表達式,或者是一個變量

{% directives %}

其餘的模板指令

{# … #}

在模板中要註釋python表達式的運行,須要使用這個模板語法

{{! {%! {#!

若是不想執行內容,須要在頁面上打印出模板符號,只須要加上感嘆號( ! )便可。輸出源代碼

控制語句:if判斷

在模板中可使用 if 判斷   注意:最後須要以 {% end %} 結尾

實例以下:

<body>
    歡迎 {{ username }} 登錄
    <br>
    {{ 1+1 }}
    <br>
    {{ time.ctime() }}
    <!--兩個大括號能夠運行python的表達式-->
    <br>
    <br>
<!--控制流程-->   {#% %#}
    {% if username !='' %}
        歡迎 {{ username }} 登錄
    {% else %}
        請登陸
    {% end %}
    <br>
    <br>
    <!--{{ time.ctime() }} 這種方式會在後臺仍然執行 下面使用{# #}註釋語法-->
    {# time.ctime() #}

    <br>
    <!--不進行運算,將表達式顯示在頁面-->
    {{! 1+1 }}  

</body>

循環語句:

for 循環:

在 tornado 模板中可使用 for 循環 注意:最後須要以 {% end %} 結尾

while 循環:

在 tornado 模板中可使用 while 循環 注意:最後須要以 {% end %} 結尾

python文件中傳入變量:

class TemHandler(RequestHandler):
    def get(self):
        self.write('hello world')
    def post(self):
        urllist=[
            ('http://www.baidu.com','百度一下'),
            ('http://www.zhihu.com','知乎'),
            ('http://www.google.com','谷歌'),
        ]
        name = self.get_argument('name','')
        self.render('02-templates.html',
                    username= name,
                    time = time,
                    urllist = urllist,
                    )

模板文件展現:

<body>
     {% for i in urllist %}
    <a href="{{ i[0] }}" target="_blank">{{ i[1] }}</a>
    <br>
    {% end %}
    <br>
    <!--須要提早設置-->
    {% set a =0 %}
    {% while a < 8 %}
        {{ a }} <br>
    {% set a+=1 %}
    {% end %}
</body>

選擇模板類型:File--->Setting--->Python Template Languages  (jinja2)

模板轉義

轉義:

頁面並無解析,只是看成一個字符串,直接在頁面上打印出來 tornado默認是自動的轉義,傳入的數據都會看成字符串,不會被瀏覽器解析

class TemHandler(RequestHandler):
    def get(self):
        pass
    def post(self):
        atga ='''
        <a href="http://www.baidu.com" target="_blank">百度一下,你就知道</a>
        '''
        self.render('02-templates.html',
                    atga = atga
                    )

application = tornado.web.Application(
    handlers=[
        (r'/',MainHandler),
        (r'/tem',TemHandler),
    ],
    debug=True,
    template_path = 'templates',
    static_path='static',
    autoescape = None, #全局取消轉義
)

模板.html

<!DOCTYPE html>
{#取消整個頁面的轉義#}
{#{% autoescape None %}#}
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Templates</title>
</head>
<body>
{#    raw 局部取消轉義#}
{#    {%  raw atga %}#}
    <br>
{#    批量取消轉義兩種方法,1。取消整個頁面的轉義。2.取消整個項目的轉義#}
    {{ atga }}
    {{ atga }}
    {{ atga }}
    {{ atga }}
    {{ atga }}
    {{ atga }}
{#    局部開啓轉義#}
    {{ escape(atga) }}
</body>
</html>

 

靜態文件引入

設置靜態資源目錄

application = tornado.web.Application(
    handlers=[
        (r'/',MainHandler),
        (r'/tem',TemHandler),
    ],
    debug=True,
    template_path = 'templates',
    static_path='static',   #設置靜態資源文件位置
    autoescape = None, #全局取消轉義
)

模板中引入靜態資源:static_url爲內置方法

兩種引入方式

<body>
{% if username !='' %}
    歡迎 {{ username }} 登陸
<img src="static/images/01.jpg" width="200px" alt="">
<img src="{{ static_url('images/02.webp') }}" width="200px" alt="">
{% else %}
    親,請登陸
{% end %}
</body>

兩種靜態資源引入的不一樣:

相關文章
相關標籤/搜索