摘要:控制結(jié)構(gòu)條件控制語句循環(huán)還支持宏。宏類似于代碼中的函數(shù)。在指令之后,基模板中的個(gè)塊被重新定義,模板引擎會(huì)將其插入適當(dāng)?shù)奈恢?。初始化之后,就可以在程序中使用一個(gè)包含所有文件的基模板。之前版本的模板中的歡迎信息,現(xiàn)在就放在這個(gè)頁面頭部。
四、模板
FMTV F:form表單 M:Model模型(數(shù)據(jù)庫) T:Template模板 V:view視圖(路由)1、渲染模板
模板是一個(gè)包含響應(yīng)文本的文件,其中包含用占位變量表示的動(dòng)態(tài)部分,其具體值 只在請(qǐng)求的上下文中才能知道。 使用真實(shí)值替換變量,再返回最終得到的響應(yīng)字符 串,這一過程稱為渲染??梢允褂?render_template() 方法來渲染模板。你需要做 的一切就是將模板名和你想作為關(guān)鍵字的參數(shù)傳入模板的變量。 Flask 會(huì)在 templates 文件夾里尋找模板。所以,如果你的應(yīng)用是個(gè)模塊,這個(gè)文 件夾應(yīng)該與模塊同級(jí);如果它是一個(gè)包,那么這個(gè)文件夾作為包的子目錄: 模板:
/application.py /templates /hello.html
包:
/application /__init__.py /templates /hello.html
【hello.html】
Hello World!
from flask import render_template @app.route("/hellotemplate/") def hellotemplate(): return render_template("hello.html")
模板引擎
Flask使用了一個(gè)名為 Jinja2 的強(qiáng)大模板引擎
{% ... %} Jinja語句,例如判斷、循環(huán)語句
{{ ... }} 變量,會(huì)顯示在瀏覽器中
{# ... #} 注釋,不會(huì)輸出到瀏覽器中
2、變量規(guī)則在模板中使用的 {{ name }} 結(jié)構(gòu)表示一個(gè)變量,它是一種特殊的占位符,告訴
模板引擎這個(gè)位置的值從渲染模板時(shí)使用的數(shù)據(jù)中獲取。
【helloname.html】
Hello, {{ name }}!
@app.route("/hellotemplate/") def helloname(name): return render_template("helloname.html",name = name)
可以使用過濾器修改變量,過濾器名添加在變量名之后,中間使用豎線分隔。
3、控制結(jié)構(gòu)1、條件控制語句
【if.html】
{% if name %} hello, {{name}} {% else %} hello, world! {% endif %}
2、 for 循環(huán)
【for.html】
{% for a in range(10) %}
@app.route("/for/") def fortemplate(): return render_template("for.html")- a
{% endfor %}
3、Jinja2 還支持宏(macro) 。宏類似于 Python 代碼中的函數(shù)(def)。
【macro.html】
{% macro myprint(A) %} this is {{ A }} {% endmacro %} {{ myprint(A) }} @app.route("/macro/") def macrotamplate(a): return render_template("macro.html",A = a)
為了重復(fù)使用宏,我們可以將其保存在多帶帶的文件中,然后在需要使用的模板中導(dǎo)入:
【macro2.html】
{% from "macro.html" import myprint %} {{ myprint(A) }} @app.route("/macro2/") def macro2template(a): return render_template("macro2.html",A = a)
4、包含(include)
【include.html】
{% include "macro.html" %} @app.route("/include/") def includetemplate(a): return render_template("include.html",A = a)
【注意】
包含進(jìn)來的文件里的所有變量也包含進(jìn)來了,需要在視圖函數(shù)中指定
首先,創(chuàng)建一個(gè)名為 base.html 的基模板:
【base.html】
{% block head %}{% block title %} {% endblock %} - My Application {% endblock %} {% block body %} {% endblock %}
block 標(biāo)簽定義的元素可在衍生模板中修改。下面這個(gè)示例是基模板的衍生模板:
【extend.html】
% extends "base.html" %} {% block title %}Index{% endblock %} {% block head %} {{ super() }}super
{% endblock %} {% block body %}Hello, World!
{% endblock %}
extends 指令聲明這個(gè)模板衍生自 base.html。在 extends 指令之后,基模板中的 3個(gè)塊被重新定義,模板引擎會(huì)將其插入適當(dāng)?shù)奈恢?。如果想添加?nèi)容到在父模板 內(nèi)已經(jīng)定義的塊,又不想失去父模板里的內(nèi)容,可以使用super函數(shù)5、使用Flask-Bootstrap
Flask-Bootstrap 使用 pip安裝:
(venv) $ pip install flask-bootstrap
Flask 擴(kuò)展一般都在創(chuàng)建程序?qū)嵗缶统跏蓟?br>初始化 Flask-Bootstrap 之后,就可以在程序中使用一個(gè)包含所有 Bootstrap 文件
的基模板。
【boootstrap.html】
{% extends "bootstrap/base.html" %} {% block title %}Flasky{% endblock %} {% block navbar %}{% endblock %} {% block content %}{% endblock %}Hello, {{ name }}!
Jinja2 中的 extends 指令從 Flask-Bootstrap 中導(dǎo)入 bootstrap/base.html,從而 實(shí)現(xiàn)模板繼承。 Flask-Bootstrap 中的基模板提供了一個(gè)網(wǎng)頁框架,引入了 Bootstrap 中的所有 CSS 和JavaScript 文件?;0逯卸x了可在衍生模板中重定義的塊。 block 和 endblock 指令定義的塊中的內(nèi)容可添加到基模板中。 上面這個(gè) boootstrap.html 模板定義了 3 個(gè)塊,分別名為 title、 navbar 和 content。 這些塊都是基模板提供的, 可在衍生模板中重新定義。 title 塊的作用很明顯,其中 的內(nèi)容會(huì)出現(xiàn)在渲染后的 HTML 文檔頭部,放在標(biāo)簽中。 navbar 和 content 這兩個(gè)塊分別表示頁面中的導(dǎo)航條和主體內(nèi)容。在這個(gè)模板中, navbar 塊使用 Bootstrap 組件定義了一個(gè)簡單的導(dǎo)航條。content 塊中有個(gè) 容器,其中包含一個(gè)頁面頭部。 之前版本的模板中的歡迎信息,現(xiàn)在就放在這個(gè)頁面頭部。from flask_bootstrap import Bootstrap bootstrap = Bootstrap(app) @app.route("/bootstrap/") def bootstraptemplate(name): return render_template("boootstrap.html",name = name) 【注意】
很多塊都是 Flask-Bootstrap 自用的,如果直接重定義可能會(huì)導(dǎo)致一些問題。例如,
Bootstrap 所需的文件在 styles 和 scripts 塊中聲明。如果程序需要向已經(jīng)有內(nèi)
容的塊中添加新內(nèi)容,必須使用Jinja2 提供的 super() 函數(shù)。例如,如果要在衍生
模板中添加新的 JavaScript 文件,需要這么定義 scripts 塊:{% block scripts %} {{ super() }} {% endblock %}6、自定義錯(cuò)誤頁面【templates/404.html】
Page is not Found
@app.errorhandler(404) def page_not_found(e): return render_template("404.html"), 404【templates/base.html: 包含導(dǎo)航條的程序基模板】
{% extends "bootstrap/base.html" %} {% block title %}Flasky{% endblock %} {% block head %} {{ super() }} {% endblock %} {% block navbar %}{% endblock %} {% block content %}{% block page_content %}{% endblock %}{% endblock %} {% block scripts %} {{ super() }} {{ moment.include_moment() }} {% endblock %}這個(gè)模板的 content 塊中只有一個(gè)
容器,其中包含了一個(gè)名為
page_content 的新的空塊,塊中的內(nèi)容由衍生模板定義。【templates/404.html:使用模板繼承機(jī)制自定義 404 錯(cuò)誤頁面】
{% extends "base.html" %} {% block title %}Flasky - Page Not Found{% endblock %} {% block page_content %}{% endblock %}Not Found
templates/boootstrap.html 現(xiàn)在可以通過繼承這個(gè)基模板來簡化內(nèi)容:
【 templates/boootstrap.html: 使用模板繼承機(jī)制簡化頁面模板】{% extends "base.html" %} {% block title %}Flasky{% endblock %} {% block page_content %}7、靜態(tài)文件{% endblock %}Hello, {{ name }}!
默認(rèn)設(shè)置下, Flask 在程序根目錄中名為 static 的子目錄中尋找靜態(tài)文件。
如果需要,可在static 文件夾中使用子文件夾存放文件。給靜態(tài)文件生成
URL ,使用特殊的 "static" 端點(diǎn)名:
【westeros.html】@app.route("/westeros/") def westeros(): return render_template("westeros.html")這個(gè)文件應(yīng)該存儲(chǔ)在文件系統(tǒng)上的 static/westeros.jpg 。
8、使用Flask-Moment本地化日期和時(shí)間lask-Moment 是一個(gè) Flask 程序擴(kuò)展,能把moment.js 集成到 Jinja2 模
板中。 Flask-Moment 可以使用 pip 安裝:(venv) $ pip install flask-moment這個(gè)擴(kuò)展的初始化方法和Bootstrap一樣,以程序?qū)嵗齛pp為參數(shù):
from flask_moment import Moment moment = Moment(app)除了 moment.js, Flask-Moment 還依賴 jquery.js。要在 HTML 文檔的某個(gè)
地方引入這兩個(gè)庫,可以直接引入,這樣可以選擇使用哪個(gè)版本,也可使用擴(kuò)
展提供的輔助函數(shù),從內(nèi)容分發(fā)網(wǎng)絡(luò)(Content Delivery Network, CDN)中
引入通過測試的版本。 Bootstrap 已經(jīng)引入了 jquery.js, 因此只需引入
moment.js即可。
【base.html中增加了】{% block scripts %} {{ super() }} {{ moment.include_moment() }} {% endblock %}【moment.html】
{% extends "base.html" %} {% block page_content %}Hello World!
The local date and time is {{moment(current_time).format("LLL")}}.
That was {{moment(current_time).fromNow(refresh = true)}}.
{% endblock %}把變量current_time 傳入模板進(jìn)行渲染
from datetime import datetime @app.route("/moment/") def momenttemplate(): return render_template("moment.html",current_time = datetime.utcnow())文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/40796.html
相關(guān)文章
發(fā)表評(píng)論
0條評(píng)論
閱讀 1007·2023-04-26 01:47
閱讀 1685·2021-11-18 13:19
閱讀 2056·2019-08-30 15:44
閱讀 670·2019-08-30 15:44
閱讀 2310·2019-08-30 15:44
閱讀 1246·2019-08-30 14:06
閱讀 1433·2019-08-30 12:59
閱讀 1909·2019-08-29 12:49