[英]Chameleon macros without Pyramid
這是我在Pyramid中使用的一些代碼,用於將宏加載到Chameleon模板中:
@subscriber(BeforeRender)
def add_base_templates(event):
"""Define the base templates."""
main_template = get_renderer('../templates/restaurant.pt').implementation()
event.update({ 'main_template': main_template })
沒有金字塔我將如何實現相同目標? 例如,在此代碼中:
from chameleon import PageTemplateFile
path = os.path.dirname(__file__)
lizard = PageTemplateFile(os.path.join(path, '..', 'emails', template+'.pt'))
html = lizard(**data)
讓我們看一下您的代碼的作用; 要在金字塔外部使用宏,您要做的就是復制它。
當您在金字塔中調用.implementation()
時,實際上是在獲取已加載正確模板路徑的PageTemplateFile
實例。
BeforeRender
事件使您可以從視圖修改dict響應,然后在add_base_templates
事件處理程序中添加一個名為main_template
的新條目。
將這兩者結合起來,可以在您自己的代碼中獲得相同的效果,並在調用lizard
模板時傳入main_template
宏模板:
from chameleon import PageTemplateFile
path = os.path.dirname(__file__)
main_template = PageTemplateFile(os.path.join(path, '..', 'templates', 'restaurant.pt'))
lizard = PageTemplateFile(os.path.join(path, '..', 'emails', template+'.pt'))
html = lizard(main_template=main_template, **data)
真的,這就是全部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.