簡體   English   中英

不帶金字塔的變色龍宏

[英]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)

讓我們看一下您的代碼的作用; 要在金字塔外部使用宏,您要做的就是復制它。

  1. 當您在金字塔中調用.implementation()時,實際上是在獲取已加載正確模板路徑的PageTemplateFile實例。

  2. 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM