将Pyramid中多个视图的渲染内容注入一个基本模板

时间:2012-02-15 08:39:07

标签: python pyramid chameleon template-tal template-metal

我想做类似下面的事情。

我配置了以下路线:

config.add_route('home', '/')
config.add_route('foo', '/foo')

以下观点:

@view_config(route_name='home', renderer='templates/home.pt')
def home_view(request):
    return {...}

@view_config(route_name='foo', renderer='templates/foo.pt')
def foo_view(request):
    return {...}

有一个基本模板'templates / base.pt':

<!DOCTYPE html>
<html>
<head></head>
<body>
    Welcome ${user_id}<br>
    <a href="/foo">Foo</a><br>
    <div id="content">
        <!-- Inject rendered content here from either / or /foo --> 
    </div>
</body>
</html>

现在在我的观点中,我想将以下内容注入ID为“content”的div:

<!-- templates/home.pt -->
<div id="home-content">Home content</div>

<!-- templates/foo.pt -->
<div id="foo-content">Foo content</div>

我如何更改上面的 home_view foo_view ,以便他们可以将自己的模板(home.pt,foo.pt)注入base.pt?不知何故,我还需要将诸如 $ {user_id} 之类的数据传输到base.pt中。在定义我的视图时,我正在使用包装器参数,但无法弄清楚它是如何工作的。

1 个答案:

答案 0 :(得分:3)

您可以通过多种方式实现这一目标(例如Using ZPT Macros in PyramidChameleon documentation introduction)。

在您的简单案例中,我认为这是最快的方法:首先,将您的base.pt文件更改为:

<!DOCTYPE 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">
<head></head>
<body>
    Welcome ${user_id}<br>
    <a href="/foo">Foo</a><br>
    <div id="content">
        <tal:block metal:define-slot="content">
        </tal:block>
    </div>
</body>
</html>

这定义了变色龙宏的content个插槽。

您的foo.pt可能如下所示:

<metal:main
    xmlns:tal="http://xml.zope.org/namespaces/tal"
    xmlns:metal="http://xml.zope.org/namespaces/metal"
    use-macro="load: base.pt">
    <tal:block metal:fill-slot="content">
        <div id="foo-content">Foo content</div>
    </tal:block>
</metal:main>

请注意use-macro="load: base.pt行。 home.pt应遵循相同的模式。 user_id和其他模板变量可供宏使用,因此,例如,如果您将user_id设置为USER/foo将呈现:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
    Welcome USER<br>
    <a href="/foo">Foo</a><br>
    <div id="content">
        <div id="foo-content">Foo content</div>
    </div>
</body>
</html>