要在 Pyramid 中使用 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 中包含插槽“content”的宏名为“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
我可以解释更多,但请查看文档。这里描述了一个像上面这样的简单情况。
- 在 Pyramid 中使用 ZPT 宏 http://docs.pylonsproject.org/projects/pyramid-chameleon/en/latest/#using-zpt-macros-in-pyramid
完整的教程也介绍了这个主题。第二个链接将增加您的知识。
- 制作主模板 https://web.archive.org/web/20131018092639/http://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/humans/creatingux/step05/index.html
- 可重复使用的模板宏 https://web.archive.org/web/20131018092639/http://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/humans/creatingux/step10/index.html
之后金字塔文档将提供更多详细信息。欢迎来到金字塔。