這是Python中最簡單,重量最輕的html模板引擎,可用於生成自定義電子郵件通訊。python的輕量級模板引擎
回答
對於一個非常小的模板任務,Python本身並沒有那麼糟糕。例如:
def dynamic_text(name, food):
return """
Dear %(name)s,
We're glad to hear that you like %(food)s and we'll be sending you some more soon.
""" % {'name':name, 'food':food}
從這個意義上講,您可以在Python中使用字符串格式來進行簡單的模板化。這就像它的輕量級一樣。
如果你想深入一點,Jinja2是許多人認爲最「設計友好」(閱讀:簡單)的模板引擎。
你也可以看看Mako和Genshi。最終,選擇是你的(有什麼你想要的功能,並與你的系統很好地集成)。
我認爲Werkzeug Mini Templates很適合這項法案。
在Github上的the source code。
*注意*:兩個鏈接無效截止至* Sun 2017-04-02T00:41:01 * – dreftymac 2017-04-02 07:41:28
有什麼不對嗎string.Template?這是標準的Python分配和PEP 292覆蓋:
from string import Template
form=Template('''Dear $john,
I am sorry to imform you, $john, but you will not be my husband
when you return from the $theater war. So sorry about that. Your
$action has caused me to reconsider.
Yours [NOT!!] forever,
Becky
''')
first={'john':'Joe','theater':'Afgan','action':'love'}
second={'john':'Robert','theater':'Iraq','action':'kiss'}
third={'john':'Jose','theater':'Korean','action':'discussion'}
print form.substitute(first)
print form.substitute(second)
print form.substitute(third)
str.format()比string.Template功能強大得多,但卻非常簡單。 – Apalala 2013-02-28 14:37:14
試用一下蟒蛇微模板:
https://github.com/diyism/python-micro-template
使用實例(kivy):
import python_micro_template
...
kvml=open('example_kivy_scrollview.kvml', 'r').read()
kvml=python_micro_template.tpl.parse(kvml)
grid=Builder.load_string(kvml)
...
模板實例(kvml):
<:for i in range(30):#{#:>
Button:
text: '<:=i:><:for j in range(6):#{#:><:=j:><:#}#:>'
size: 480, 40
size_hint: None, None
<:#}#:>
- 1. Android輕量級HTML模板引擎
- 2. asp.net的輕量級搜索引擎
- 3. Python模板引擎
- 4. 輕量級物理引擎Android
- 5. 輕量級Java報告引擎
- 6. 免費的輕量級模板系統
- 7. Python 3模板引擎
- 8. 升級版本smarty模板引擎
- 9. 模板引擎
- 10. Python的網頁模板引擎
- 11. Django的模板引擎變量
- 12. Javascript模板引擎?
- 13. 模板引擎JS
- 14. 笨模板引擎
- 15. asp.net模板引擎
- 16. JavaScript模板引擎
- 17. 輕量級推理引擎可以與Ruby接口
- 18. 如何從模板引擎中輕鬆訪問MongoDB ObjectId日期?
- 19. 模板引擎的node.js
- 20. node.js的模板引擎
- 21. 模板引擎的node.js
- 22. jQuery的模板引擎
- 23. 一個輕量級的可模板WPF形狀
- 24. 輕量級重量級
- 25. GWT和模板引擎
- 26. 定製PHP模板引擎
- 27. 模板引擎:包括
- 28. Aurelia模板引擎撰寫
- 29. 文檔模板引擎
- 30. CakePHP模板引擎2.3
我同意,很多時候Python的格式化工具正是需要的。你可以做一些像`「Hello {foo}」格式(foo =「World」)`。也可以考慮Python'string`模塊:http://docs.python.org/library/string.html#template-strings 但是,一旦你開始需要一些條件或迭代邏輯in-template,你需要一些東西正如其他答案中所建議的那樣。 – 2010-12-10 06:07:54