【问题标题】:Chameleon macros without Pyramid没有金字塔的变色龙宏
【发布时间】:2012-07-29 04:04:02
【问题描述】:

这是我在 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 })

如果没有 Pyramid,我将如何实现同样的目标?例如,在这段代码中:

from chameleon import PageTemplateFile

path = os.path.dirname(__file__)
lizard = PageTemplateFile(os.path.join(path, '..', 'emails', template+'.pt'))
html = lizard(**data)

【问题讨论】:

    标签: python pyramid chameleon


    【解决方案1】:

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

    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)
    

    这就是它的全部内容,真的。

    【讨论】:

      猜你喜欢
      • 2011-08-27
      • 2013-04-02
      • 1970-01-01
      • 2023-03-03
      • 2013-07-14
      • 2011-12-07
      • 1970-01-01
      • 2011-12-25
      • 1970-01-01
      相关资源
      最近更新 更多