old blog,

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

Izhar Firdaus Izhar Firdaus Follow Support May 08, 2010 · 2 mins read
Using deliverance as WSGI pipeline to elegantly theme a repoze.bfg project
Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.
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="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!
Written by Izhar Firdaus Follow Support
I'm a system architect, data engineer and developer advocate with passion in Free / Open Source software, entrepreneurship, community building, education and martial art. I take enjoyment in bridging and bringing together different FOSS technologies to help businesses and organizations utilize IT infrastructure to aid and optimize their business and organizational process.

repoze.bfg - Introduction and bootstrapping it on buildout

2 months without a post here .. T_T ... must .. restart .. blogging ...Anyway, I've been poking around with repoze.bfg since last wee...

In old blog, May 08, 2010

« Previous Post

list as default value in class __init__() - watchout for surprise

Note to self: don't use list as a default value in class methods if its going to be modified later, as it might cause some trouble.Ju...

In old blog, May 12, 2010

Next Post »