Knu*_*nut 3 macros pyramid chameleon template-tal template-metal
我想使用带有金字塔+ ZPT引擎(Chameleon)的宏.
文档说"单个页面模板可以容纳多个宏". 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>
Run Code Online (Sandbox Code Playgroud)
以及一个全局模板,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>
Run Code Online (Sandbox Code Playgroud)
到目前为止,我痛苦地发现,我不能说,use-macro="main_template.pt"因为Chameleon不会像Zope那样自动加载模板.因此,我之前必须添加load:片段.
来了use-macro="step-0".这引发了NameError step-0.我尝试macros.pt用类似的东西预加载,<tal:block tal:define="compile load: macros.pt" />但这没有帮助.
如何使用在宏摘要文件中收集的宏?
要在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}
Run Code Online (Sandbox Code Playgroud)
在渲染器将使用的模板中,您应该像这样引用宏.我假设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>
Run Code Online (Sandbox Code Playgroud)
对模板内部宏的引用如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
要获取模板中的所有宏以将它们在视图中传递到渲染模板,请将此行添加到第一个代码示例并扩展返回的字典.
macros = get_renderer('templates/main_template.pt').implementation().macros
Run Code Online (Sandbox Code Playgroud)
我可以解释更多,但看看文档.这里描述了一个类似上面的简单情况.
完整的教程也介绍了这个主题.第二个链接将提升您的知识.
之后金字塔文档将提供更多细节.欢迎来到金字塔.