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