[英]Chameleon macros without Pyramid
Here's some code I use in Pyramid to load macros into my Chameleon templates: 这是我在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 })
How would I achieve the same without Pyramid? 没有金字塔我将如何实现相同目标? For example, in this code:
例如,在此代码中:
from chameleon import PageTemplateFile
path = os.path.dirname(__file__)
lizard = PageTemplateFile(os.path.join(path, '..', 'emails', template+'.pt'))
html = lizard(**data)
Let's take a look at what your code does; 让我们看一下您的代码的作用; all you have to do to use macros outside of pyramid is to replicate this.
要在金字塔外部使用宏,您要做的就是复制它。
When you call .implementation()
in pyramid, you are essentially retrieving a PageTemplateFile
instance with the correct template path loaded. 当您在金字塔中调用
.implementation()
时,实际上是在获取已加载正确模板路径的PageTemplateFile
实例。
The BeforeRender
event let's you modify the dict response from a view, and in your add_base_templates
event handler you add a new entry named main_template
. BeforeRender
事件使您可以从视图修改dict响应,然后在add_base_templates
事件处理程序中添加一个名为main_template
的新条目。
Combine these two to get the same effect in your own code, passing in a main_template
macro template when calling your lizard
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)
That's all there is to it, really. 真的,这就是全部。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.