4

我正在使用过程来使用其他模板可以从中派生的基本模板。

如何创建多个基本模板?

4

1 回答 1

3

只需注册它们:

from pyramid.renderers import get_renderer

def add_base_template(event):
    base = get_renderer('templates/base.pt').implementation()
    base2 = get_renderer('templates/base2.pt').implementation()
    event.update({'base': base, 'base2': base2})

然后为每个页面选择要在模板中使用的内容:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      metal:use-macro="base">
    <tal:block metal:fill-slot="content">
        My awesome content.
    </tal:block>
</html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal"
      metal:use-macro="base2">
    <tal:block metal:fill-slot="content">
        Content on a totally different page.
    </tal:block>

我相信模板不必是整个 HTML 元素,因此您可以将 2 个宏扩展为相同的最终模板

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:tal="http://xml.zope.org/namespaces/tal"
      xmlns:metal="http://xml.zope.org/namespaces/metal">
    <body>
        <div metal:use-macro="section1">
            <tal:block metal:fill-slot="content">
                Content for template "section1".
            </tal:block>
        </div>
        <div metal:use-macro="section2">
            <tal:block metal:fill-slot="content">
                Content for template "section2".
            </tal:block>
        </div>
    </body>
于 2011-07-08T11:46:20.773 回答