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