2012-04-27 22 views
1

下面是一些代碼,我在金字塔使用宏加載到我的變色龍模板:變色龍宏而不金字塔

@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

讓我們來看看您的代碼的功能;在金字塔之外使用宏所需要做的就是複製這個。

  1. 當你打電話傳銷.implementation(),你基本上是檢索PageTemplateFile實例與加載正確的模板路徑。

  2. BeforeRender事件讓我們修改從視圖中的字典響應,並在您add_base_templates事件處理程序中添加了名爲main_template一個新條目。

結合這兩種讓自己的代碼相同的效果,傳遞一個main_template宏模板調用您的lizard模板時:

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) 

這一切就是這麼簡單,真的。