Kid match template fun
13 Sep 2006
I haven't yet had the opportunity to spend much time looking at Genshi (nee Markup), which will probably follow Kid as the default templating engine in TurboGears. One thing Kid lets me do that it doesn't yet seem obvious to me how to do in Genshi is this trick.
<body py:match="item.tag=='{http://www.w3.org/1999/xhtml}body'" py:attrs="item.items()"> <div py:match="item.tag=='{http://www.w3.org/1999/xhtml}div' and item.get(u'id')=='header'" py:attrs="item.items()" id="header"> <?python h1 = [e for e in item[:] if e.tag == "{http://www.w3.org/1999/xhtml}h1"] if h1: h1 = h1[0] item = [e for e in item[:] if e.tag != "{http://www.w3.org/1999/xhtml}h1"] else: h1 = '' ?> <div id="nav"> <div py:replace="item[:]" /> <span py:if="tg.config('identity.on',False) and not 'logging_in' in locals()" id="pageLogin"> <span py:if="tg.identity.anonymous"> <a href="${tg.url('/login')}">Login</a> </span> <span py:if="not tg.identity.anonymous"> <a href="${tg.url('/logout')}">Logout</a> </span> </span> <div py:if="not tg.identity.anonymous" class="welcome">Welcome ${tg.identity.user.display_name}.</div> </div> <h1 py:replace="h1">Favourites</h1> </div>
Basically, what this does is allow a template to describe what additional links should be included in the navigation easily, by placing the links in the div tag with the id header, like so:
<div id="header"> <h1> ${photo.name} </h1> <a href="#" target="#editphoto" REL="ibox?ibox_type=1&height=300&width=500" title="Edit photo" py:if="not tg.identity.anonymous">Edit photo</a> </div>
The match template above discovers that it matches the div with id header, and then picks out the h1 tag. It then adds any additional navigation mentioned in that header div, and then adds login and logout options. Finally, if the user is logged in, welcome them to the system.
On most pages, this is blank, but on some pages I, for example, get to add a "add photo" link:

3 old-style comments
Florian — September 14, 2006 at 09:10 AM.
Karl Guertin — September 14, 2006 at 06:53 PM.
This is the Page Header
Hi, I'm Neil Blakey-Milner, a technology generalist most interested by open source, security, and building scalable systems (especially if I get to use the Python programming language).
I "work" at the incredibly fun web-sites-for-everyone startup SynthaSite based in beautiful Cape Town, South Africa.
What I do
I was the main instigator behind this 2-day unconference in December 2007 in Cape Town. There'll be another one in Cape Town around July.
I help out in making these geek social events happen every two months in Cape Town since March 2007.
I make sure that the systems run smoothly at this Cape Town-born Web 2.0 startup.
I speak publically on a number of topics - generally related to technical aspects of open source software or on software development.
I've written a few software projects of my own over the years (although mostly I'm the only user of them).
I'm far from an artist, but I enjoy capturing my experiences on camera.
Recent Entries
Archives
${photo.name}
Edit photoKarl Guertin — September 14, 2006 at 06:55 PM.