新聞中心
當(dāng)需要使用模板語(yǔ)言來(lái)編寫(xiě) Python Web 應(yīng)用時(shí),有很多健壯的解決方案。

有 Jinja2、Genshi 和 Mako。甚至還有 Chameleon 之類(lèi)的解決方案,雖然有些陳舊,但仍被 Pyramid 框架推薦。
Python 已經(jīng)存在了很長(zhǎng)時(shí)間。此時(shí),在系統(tǒng)的深處,它積累了一些幾乎被遺忘的模板語(yǔ)言,它們都是值得一試的。
這些語(yǔ)言就像桉樹(shù)上可愛(ài)的考拉一樣,在自己的生態(tài)圈里快樂(lè)地生活著,有時(shí)也會(huì)有危險(xiǎn)的工作,這些都是很少有人聽(tīng)說(shuō)過(guò)的模板語(yǔ)言,使用過(guò)的應(yīng)該更少。
3. string.Template
你是否曾經(jīng)想過(guò):“如何獲得一種沒(méi)有任何特性的模板語(yǔ)言,而且同時(shí)也不需要 pip install 安裝任何東西?” Python 標(biāo)準(zhǔn)庫(kù)已經(jīng)為你提供了答案。雖然沒(méi)有循環(huán)和條件,但 string.Template 類(lèi)是一種最小的模板語(yǔ)言。
使用它很簡(jiǎn)單。
- >>> import string
- >>> greeting = string.Template("Hello, $name, good $time!")
- >>> greeting.substitute(name="OpenSource.com", time="afternoon")
- 'Hello, OpenSource.com, good afternoon!'
2. twisted.web.template
你會(huì)給一個(gè)包羅萬(wàn)象的庫(kù)送什么禮物?
當(dāng)然,不是模板語(yǔ)言,因?yàn)樗呀?jīng)有了。twisted.web.template 中嵌套了兩種模板語(yǔ)言。一種是基于 XML 的,并有一個(gè)很棒的文檔。
但是它還有另一種,一種基于使用 Python 作為領(lǐng)域特定語(yǔ)言(DSL)來(lái)生成 HTML 文檔。
它基于兩個(gè)原語(yǔ):包含標(biāo)簽對(duì)象的 twisted.web.template.tags 和渲染它們的 twisted.web.template.flattenString。由于它是 Twisted 的一部分,因此它內(nèi)置支持高效異步渲染。
此例將渲染一個(gè)小頁(yè)面:
- async def render(reactor):
- my_title = "A Fun page"
- things = ["one", "two", "red", "blue"]
- template = tags.html(
- tags.head(
- tags.title(my_title),
- ),
- tags.body(
- tags.h1(my_title),
- tags.ul(
- [tags.li(thing) for thing in things],
- ),
- tags.p(
- task.deferLater(reactor, 3, lambda: "Hello "),
- task.deferLater(reactor, 3, lambda: "world!"),
- )
- )
- )
- res = await flattenString(None, template)
- resres = res.decode('utf-8')
- with open("hello.html", 'w') as fpout:
- fpout.write(res)
該模板是使用 tags. 來(lái)指示層次結(jié)構(gòu)的常規(guī) Python 代碼。原生支持渲染字符串,因此任何字符串都正常。
要渲染它,你需要做的是添加調(diào)用:
- from twisted.internet import task, defer
- from twisted.web.template import tags, flattenString
- def main(reactor):
- return defer.ensureDeferred(render(reactor))
最后寫(xiě)上:
- task.react(main)
只需 3 秒(而不是 6 秒),它將渲染一個(gè)不錯(cuò)的 HTML 頁(yè)面。在實(shí)際中,這些 deferLater 可以是對(duì) HTTP API 的調(diào)用:它們將并行發(fā)送和處理,而無(wú)需付出任何努力。我建議你閱讀關(guān)于更好地使用 Twisted。不過(guò),這已經(jīng)可以工作了。
1. Quixote
你會(huì)說(shuō):“但是 Python 并不是針對(duì) HTML 領(lǐng)域而優(yōu)化的領(lǐng)域特定語(yǔ)言。” 如果有一種語(yǔ)言可以轉(zhuǎn)化到 Python,但是更適合定義模板,而不是像 Python 那樣按原樣解決呢?如果可以的話,請(qǐng)使用“Python 模板語(yǔ)言”(PTL)。
編寫(xiě)自己的語(yǔ)言,有時(shí)被說(shuō)成是一個(gè)攻擊假想敵人的唐吉坷德項(xiàng)目。當(dāng) Quixote(可在 PyPI 中找到)的創(chuàng)造者決定這樣做時(shí),并沒(méi)有受此影響。
以下將渲染與上面 Twisted 相同的模板。警告:以下不是有效的 Python 代碼:
- import time
- def render [html] ():
- my_title = "A Fun page"
- things = ["one", "two", "red", "blue"]
- "
" - my_title
- "
"
- my_title
- ""
- "
"
- for thing in things:
- "
- "
- thing
- "
"- "
"
- time.sleep(3)
- (lambda: "Hello ")()
- time.sleep(3)
- (lambda: "world!")()
- ""
- ""
- def write():
- result = render()
- with open("hello.html", 'w') as fpout:
- fpout.write(str(result))
但是,如果將它放到 template.ptl 文件中,那么可以將其導(dǎo)入到 Quixote 中,并寫(xiě)出可以渲染模板的版本:
- >>> from quixote import enable_ptl
- >>> enable_ptl()
- >>> import template
- >>> template.write()
Quixote 安裝了一個(gè)導(dǎo)入鉤子,它會(huì)將 PTL 文件轉(zhuǎn)換為 Python。請(qǐng)注意,此渲染需要 6 秒,而不是 3 秒。你不再獲得自由的異步性。
Python 中的模板太多
Python 庫(kù)的歷史悠久且曲折,其中一些庫(kù)可以或多或少都能達(dá)到類(lèi)似結(jié)果(例如,Python 包管理)。
我希望你喜歡探索這三種可以用 Python 創(chuàng)建模板的方式。另外,我建議從這三個(gè)庫(kù)之一開(kāi)始了解。
文章標(biāo)題:你應(yīng)該(或許)沒(méi)使用過(guò)的3種Python模板語(yǔ)言
文章路徑:http://m.5511xx.com/article/dheecij.html


咨詢(xún)
建站咨詢(xún)
