In Plone/Grok template inheritance can be done by registering main_template as a view, and using a template that fill the slots in main_template such as this:
<html xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
metal:use-macro="context/main_template/macros/master">
<head></head>
<body>
<div metal:fill-slot="main">
Some content
</div>
</body>
</html>
However, by default, repoze.bfg does not support getting view macro from a context. So a PageTemplate object need to by passed by a view in order to use the macro from the template. Eg:
from repoze.bfg.chameleon_zpt import get_template
def my_view(context,request):
main_template = get_template('templates/main_template.pt')
return dict(main_template=main_template)
So the view would be using:
<html xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
metal:use-macro="main_template.macros['master']">
Simple enough, but something came up my mind. "How about I theme this using Deliverance?"
What is Deliverance?
In simple terms, its a theming proxy which also available as a WSGI middleware. It allow developers to theme different systems without the need to know the internal of the systems. More information here.
Setting up Deliverance in repoze.bfg buildout
So lets get to the fun stuff.
I'm assuming that repoze.bfg is installed in a buildout, and a project called 'helloworld' is installed in the buildout, similar to the one I've shown in my previous post
First, add the egg:
[buildout]
...
[repoze]
...
eggs =
...
Deliverance
...
and run
./bin/buildout -vvv
Now, configure the Deliverance filter.
Open
src/helloworld/helloworld.ini
, rename [app:main]
to [app:helloworld]
and add these:[filter:deliverance]
use = egg:Deliverance#main
theme_uri = /static/layout.html
rule_uri = /static/rules.xml
[pipeline:main]
pipeline =
deliverance
helloworld
Afterward, create a layout.html and a rules.xml in
helloworld/templates/static
with these contents:layout.html
<html>
<head>
<title>The Theme</title>
</head>
<body>
<h1>A theme</h1>
<div id="content">
</div>
<hr>
</body>
</html>
rules.xml
<ruleset>
<rule>
<replace content="children:body" theme="children:#content" if-content="not:#content"/>
<replace content="children:#content" theme="children:#content"/>
</rule>
</ruleset>
Documentation of the rules markup are here: http://packages.python.org/Deliverance/configuration.html#rule
Now you're ready for profit :D
Start the server using
./bin/paster serve src/helloworld/helloworld.ini
Happy Hacking!