Friday, August 24, 2007

Genshi Templates: include and select()

Hint for today: If you don't know Xpath select stuff at all (like I didn't a couple minutes ago) and you want your Turbogears templates to be a little more intelligent about column content placement, you can do something like this:

<!-- in master.html -->
<div id="centercontent">
<div id="status_block" py:if="tg_flash"
class="flash" py:content="tg_flash">
</div>
<div
py:replace=
"select('//div[@id=\'centercontent\']/*|text()')">
</div>
<div id="rightcontent">
<div
py:replace=
"select('//div[@id=\'rightcontent\']/*')">
</div>

<!-- in welcome.html -->
<div id="centercontent">
<!-- all your base here -->
</div>
<div id="rightcontent">
<!-- navigation stuff here -->
</div>

Breaking down the Xpath select statement a little more (removing the quote escapes):
//div[@id='centercontent']/*
//div == in all 'div' elements
[@id='centercontent'] == where the (@) attribute 'id' is equal to 'centercontent'
/* == select all the children elements

1 comment:

Anonymous said...

Excellent post. Answers a ton of questions.