无金字塔变色龙宏

2024-10-05 14:25:23 发布

您现在位置:Python中文网/ 问答频道 /正文

下面是一些我在Pyramid中用于将宏加载到变色龙模板中的代码:

@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 })

没有金字塔,我怎么能达到同样的效果?例如,在本代码中:

^{pr2}$

Tags: the代码eventadd模板pyramidbasemain
1条回答
网友
1楼 · 发布于 2024-10-05 14:25:23

让我们来看看您的代码是做什么的;要在金字塔之外使用宏,您所要做的就是复制它。在

  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)

真的,就这些了。在

相关问题 更多 >