Flask入門到精通(三)

1、引入JinJa2模版html

首先咱們來對比兩段代碼,它們實現一樣的功能, 都是現實用戶信息列表:app

 1 # 第一段
 2 @app.route("/users")
 3 def get_users():
# 前面省略邏輯處理過程,獲得users列表
4 users = [{"name": "Jack", "age": 25, "city": "NewYork"}, 5 {"name": "Rose", "age": 26, "city": "Beijing"}, 6 {"name": "LiLi", "age": 23, "city": "ShangHai"}] 7 html = """ 8 <table border="1"> 9 <tr> 10 <th>姓名</th> 11 <th>年齡</th> 12 <th>城市</th> 13 </tr>""" 14 for user in users: 15 h = """<tr> 16 <td>%s</td> 17 <td>%s</td> 18 <td>%s</td> 19 </tr>"""%(user['name'], user['age'], user['city']) 20 html += h 21 html += "</table>" 22 return html 23 24 # 第二段 25 @app.route("/users") 26 def get_users_with_tempalte():
# 邏輯處理
27 users = [{"name": "Jack", "age": 25, "city": "NewYork"}, 28 {"name": "Rose", "age": 26, "city": "Beijing"}, 29 {"name": "LiLi", "age": 23, "city": "ShangHai"}] 30 return render_template("users.html", users=users)
 1 <table border="1">
 2     <tr>
 3         <th>姓名</th>
 4         <th>年齡</th>
 5         <th>城市</th>
 6     </tr>
 7 
 8     {% for user in users %}
 9     <tr>
10         <td>{{ user.name }}</td>
11         <td>{{ user.age }}</td>
12         <td>{{ user.city }}</td>
13     </tr>
14     {%endfor%}
15 
16 </table>
users.html

第一段代碼中,將業務邏輯處理與視圖展示的html都放在視圖函數裏,當業務處理邏輯或展示頁面較複雜時,整個試圖函數將會很是醜陋,難以理解且不易維護。ide

第二段代碼中, 咱們將業務邏輯處理與頁面展示分開,將展示邏輯轉移到模版中,使得代碼目的清晰,井井有條,並且在模版中咱們能夠更好的進行頁面渲染,這就是Jinja2模版引擎的魅力。函數

render_template()函數用於渲染模版,第一個參數表示模版文件名, 咱們須要在當前項目路徑下建立一個templates文件夾,用來放置html模版文件。隨後的參數都是鍵值對,表示模版中的變量對應的實際值。spa

 

2、Jinja2模版中的控制結構code

一、if-else語句htm

    

{% if name %}
    <h1>Hello, {{ name }}!</h1>
{% else %}
    <h1>Hello, Stranger!</h1>
{% endif %}

 

二、for循環(見(一)中示例)blog

三、宏定義繼承

    Jinja2中的宏,相似Python中的函數,可供須要時調用,例如對於對於上文展示用戶列表的例子,咱們能夠這樣定義:ci

    

 1 {% macro render_user(user) %}
 2     <td>{{ user.name }}</td>
 3     <td>{{ user.age }}</td>
 4     <td>{{ user.city }}</td>
 5 {% endmacro %}
 6 
 7 <table border="1">
 8     <tr>
 9         <th>姓名</th>
10         <th>年齡</th>
11         <th>城市</th>
12     </tr>
13 
14     {% for user in users %}
15     <tr>
16         {{ render_user(user) }}
17     </tr>
18     {%endfor%}
19 
20 </table>

其中:macro表示定義宏, render_user至關於函數名, user即爲參數。如果某些宏重複使用率較高, 咱們能夠將它們保存在一個單獨的html文件中,而後經過

{% import 'macros.html  as macros %}引入,相似於Python的模塊調用過程。

四、模版引用

和宏的引用相似, 咱們可將多處重複使用的模版代碼保存在單獨的文件中,而後在須要的地方引用便可{% include 'common.html' %}

五、模版繼承

模版繼承相似於Python的繼承,咱們能夠建立一個base.html模版,該模版包含基本的html塊結構,經過繼承該模版,可省去在每一個模版中都書寫類似內容的過程,而只突出當前模版的主要功能。

<!--base.html-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Jinja2 Template</title>
</head>
<body>
 {% block body %}
 {% endblock %}
</body>
</html>

<!-- users.html -->
{% extends 'base.html' %}

{% block body %}
{% macro render_user(user) %}
    <td>{{ user.name }}</td>
    <td>{{ user.age }}</td>
    <td>{{ user.city }}</td>
{% endmacro %}

<table border="1">
    <tr>
        <th>姓名</th>
        <th>年齡</th>
        <th>城市</th>
    </tr>

    {% for user in users %}
    <tr>
        {{ render_user(user) }}
    </tr>
    {%endfor%}

</table>
{% endblock %}

extends關鍵字表示繼承關係, {% block body %}...{% endblock %}實現了body部份內容的個性化展示。

相關文章
相關標籤/搜索