Saturday, May 08, 2010

Using deliverance as WSGI pipeline to elegantly theme a repoze.bfg project

I was looking around for template inheritance solution for repoze.bfg, as it seems like by default, BFG does not have context view traversal support to acquire template macros.

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=""
<div metal:fill-slot="main">
Some content

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/')
return dict(main_template=main_template)

So the view would be using:
<html xmlns:tal=""

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:
eggs =

and run ./bin/buildout -vvv

Now, configure the Deliverance filter.

Open src/helloworld/helloworld.ini , rename [app:main] to [app:helloworld] and add these:
use = egg:Deliverance#main
theme_uri = /static/layout.html
rule_uri = /static/rules.xml

pipeline =

Afterward, create a layout.html and a rules.xml in helloworld/templates/static with these contents:

<title>The Theme</title>
<h1>A theme</h1>
<div id="content">

<replace content="children:body" theme="children:#content" if-content="not:#content"/>
<replace content="children:#content" theme="children:#content"/>

Documentation of the rules markup are here:

Now you're ready for profit :D

Start the server using ./bin/paster serve src/helloworld/helloworld.ini

Happy Hacking!
Post a Comment
Locations of visitors to this page