-->

如何使用金字塔宏/ ZPT(变色龙)(how to use macros with pyramid

2019-08-21 08:24发布

我想使用金字塔+ ZPT引擎(变色龙)宏。

该文档说,“一个单一的页面模板可以容纳多个宏。” http://chameleon.readthedocs.org/en/latest/reference.html#macros-metal

因此,我定义的文件macros.pt

<div metal:define-macro="step-0">
  <p>This is step 0</p>
</div>
<div metal:define-macro="step-1">
  <p>This is step 1</p>
</div>

和全局模板main_template.pt所有的HTML的东西定义槽content

和我的视图模板progress.pt它使用main_template.pt填写插槽:

<html metal:use-macro="load: main_template.pt">
  <div metal:fill-slot="content">
    ...
    <div metal:use-macro="step-0"></div>
    ...
  </div>
</html>

到目前为止,我痛苦地发现,我不能只说use-macro="main_template.pt"因为变色龙为Zope就不会自动加载模板。 因此,我不得不添加的load:前片段。

use-macro="step-0" 。 这引发NameError了step-0 。 我试图预装macros.pt的东西,如<tal:block tal:define="compile load: macros.pt" />但这并没有帮助。

我如何使用它们聚集在宏摘要文件宏?

Answer 1:

在金字塔使用ZPT宏,你需要通过将宏模板,甚至宏本身,经过渲染的模板(从文档摘录),以使宏观模板本身可用于渲染的模板。

from pyramid.renderers import get_renderer
from pyramid.view import view_config

@view_config(renderer='templates/progress.pt')
def my_view(request):
    snippets = get_renderer('templates/macros.pt').implementation()
    main = get_renderer('templates/main_template.pt').implementation()
    return {'main':main,'snippets':snippets}

在将被渲染器所使用的模板,你应该引用这样的宏。 我假设你在包含沟槽“内容” main_template.pt宏被命名为“global_layout”。 将其更改为你的名字。

<html metal:use-macro="main.macros['global_layout']">
  <div metal:fill-slot="content">
    ...
    <div metal:use-macro="snippets.macros['step-0']"></div>
    ...
  </div>
</html>

到模板中宏的引用是这样的:

<div metal:use-macro="template.macros['step-0']">
    <div metal:fill-slot="content">
        added your content
    </div>
</div>
<div metal:define-macro="step-0">
    a placeholder for your content
    <div metal:define-slot="content">
    </div>
</div>

为了得到一个模板内的所有宏将它们一个视图中为呈现模板添加此行第一个代码示例,并延长返回的字典。

macros = get_renderer('templates/main_template.pt').implementation().macros

我可以解释更多,但看看文档。 一个简单的情况下,象上面在此描述的一个。

  • 在金字塔使用ZPT宏

一个完整的教程介绍了这一主题为好。 第二个链接将提高你的知识。

  • 制作主要模板
  • 可重复使用的模板的宏

此后金字塔文档将提供更多详细信息。 欢迎光临金字塔。



文章来源: how to use macros with pyramid / ZPT (Chameleon)