我想做类似下面的事情。
我配置了以下路线:
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中。在定义我的视图时,我正在使用包装器参数,但无法弄清楚它是如何工作的。
答案 0 :(得分:3)
您可以通过多种方式实现这一目标(例如Using ZPT Macros in Pyramid或Chameleon 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>