Jinja2實(shí)現(xiàn)模板渲染與訪問對(duì)象屬性流程詳解
1.Jinja2
1.簡(jiǎn)介
Jinja2是Python下一個(gè)被廣泛應(yīng)用的模版引擎,他的設(shè)計(jì)思想來源于Django的模板引擎,并擴(kuò)展了其語法和一系列強(qiáng)大的功能。其中最顯著的一個(gè)是增加了沙箱執(zhí)行功能和可選的自動(dòng)轉(zhuǎn)義功能,這對(duì)大多應(yīng)用的安全性來說是非常重要的。
2.Jinja2模板
Jinja 模板只是一個(gè)文本文件,可以 基于模板生成任何基于文本的格式(HTML、XML、CSV、LaTeX 等),一般用在前端的項(xiàng)目中,渲染 HTML 文件。
作為網(wǎng)絡(luò)工程師,可以將其用來批量生成網(wǎng)絡(luò)設(shè)備的配置或者其他需要批量生成文本的場(chǎng)景中。
模板包含變量或表達(dá)式,這兩者在模板求值的時(shí)候會(huì)被替換為值。模板中還有標(biāo)簽,控制模板的邏輯。模板語法的大量靈感來自于 Django 和 Python 。
2.模板渲染
- 創(chuàng)建和渲染模板的最基本方法是通過
Template
,通過創(chuàng)建一個(gè)Template
的實(shí)例Flask提供的 - render_template 函數(shù)封裝了Jinja2模板引擎
- render_template 函數(shù)的第一個(gè)參數(shù)是模板的文件名,后面的參數(shù)都是鍵值對(duì),表示向模板中傳遞的參數(shù)值
app.py
# render_template 渲染模板 from flask import Flask,render_template app = Flask(__name__) @app.route('/') def hello_world(): # put application's code here return render_template("index.html") @app.route("/blog/<blog_id>") def blog_detail(blog_id): return render_template("blog_detail.html",blog_id=blog_id,username="小程") if __name__ == '__main__': app.run()
templates/index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>小程困了</h1> </body> </html>
templates/blog_detail.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>博客詳情</title> </head> <body> <p>您的用戶名是:{{ username }}</p> <h1>您訪問的博客詳情是:{{ blog_id }}</h1> </body> </html>
效果
3.模板訪問對(duì)象屬性
app.py
# render_template 渲染模板 from flask import Flask, render_template app = Flask(__name__) class User: def __init__(self, username, email): self.username = username self.email = email @app.route('/') def hello_world(): # put application's code here user = User(username="小程", email="xxx@qq.com") person = { "username": "張三", "email": "zhangsan@qq.com" } return render_template("index.html", user=user, person=person) @app.route("/blog/<blog_id>") def blog_detail(blog_id): return render_template("blog_detail.html", blog_id=blog_id, username="小程") if __name__ == '__main__': app.run()
templates/index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>小程困了</h1> <div>{{ user.username }} / {{ user.email }}</div> <div>{{ person['username'] }} / {{ person.email }}</div> </body> </html>
效果
到此這篇關(guān)于Jinja2實(shí)現(xiàn)模板渲染與訪問對(duì)象屬性流程詳解的文章就介紹到這了,更多相關(guān)Jinja2模板渲染內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python處理中文標(biāo)點(diǎn)符號(hào)大集合
中文文本中可能出現(xiàn)的標(biāo)點(diǎn)符號(hào)來源比較復(fù)雜,通過匹配等手段對(duì)他們處理的時(shí)候需要格外小心,防止遺漏,下面小編給大家?guī)砹薖ython處理中文標(biāo)點(diǎn)符號(hào)大集合,感興趣的朋友跟隨腳本之家小編一起看看吧2018-05-05python實(shí)現(xiàn)支付寶轉(zhuǎn)賬接口
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)支付寶轉(zhuǎn)賬接口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05Python?plt.title()函數(shù)實(shí)例詳解
plt.title() 是 matplotlib 庫(kù)中用于設(shè)置圖形標(biāo)題的函數(shù),這篇文章主要介紹了Python?plt.title()函數(shù),需要的朋友可以參考下2023-03-03Python讀取excel文件中的數(shù)據(jù),繪制折線圖及散點(diǎn)圖
這篇文章主要介紹了Python讀取excel文件中的數(shù)據(jù),繪制折線圖及散點(diǎn)圖,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09