Adding a navigation bar to master.kid

0 views
Skip to first unread message

thesamet

unread,
Dec 10, 2006, 5:20:29 AM12/10/06
to TurboGears
Hi,

I'd like to add a navigation bar to master.kid which will show on all
pages and will provide the current page a distinct css class.

It is easy to do so if the handler will define some variable to help
master.kid decide which item is the selected one. But I'd like the data
to come from the template, and not the handler, and I'm not sure how to
do it. It can sure be done also with py:def on master.kid, but then it
constrains where in the XHTML the navigation bar can appear.

I thought to add a "navitem" attribute to the body tag, and then fetch
it in masker.kid from item.items(). But then this tag should be removed
from the items list, (otherwise it's going to be the output), and it's
get messy. Is there an easier way?

Nadav

Igor Foox

unread,
Dec 10, 2006, 11:02:18 AM12/10/06
to turbo...@googlegroups.com

Hi Nadav,

I do it using py:def, and it turned out to be pretty good. Here's how:

master.kid:
=========
...
<div id="sidebar" py:def="sidebar(cur)">
<li py:if="cur == 'page1'" class="foo">Page1</li>
<li py:if="not cur == 'page1'" class="bar">Page1</li>
...
</div>
...

page1.kid:
========
...
<div py:replace="sidebar( 'page1')" />
...


Another way to do it is to make use of tg.url(). I've noticed that If
I'm I py:replace
something in a child template, like above, and have tg.url() in
master.kid, then the
url will actually return the url of the child page. i.e.:

When a request comes for http://mydomain/myapp/page1, then the
following code in
master.kid:
<? python
where_am_i = tg.url("/")
?>

will actually result in where_am_i getting set to "http://mydomain/
myapp/page1".
You can use this to set the classes on your templates according to
the current page.

I'd be interested to hear other ways that people have found to do
this, since this is just
something I found out through blind experimentation. :-)

Igor

Adam Jones

unread,
Dec 10, 2006, 12:14:25 PM12/10/06
to TurboGears

You can reproduce items from an inheriting template anywhere in your
master. Here's a snippet from my master template:

<body py:match="item.tag=='{http://www.w3.org/1999/xhtml}body'"
py:attrs="item.items()">
<p id="tagline">Armchair programming at its finest.</p>
<img src="/static/images/page_header.jpg"
title="RecursiveThought.com" id="site-header"/>
<div id="content-container">
<div py:if="tg_flash" class="flash"
py:content="tg_flash"></div>
<div py:replace="[item.text]+item[:]"/>
</div>
<div id="sidebar">
<div id="navigation-sidebar" class="link-sidebar">

As you can see I am waiting until I get inside the "content-container"
div to reproduce any items from a template inheriting the master. Any
templates that inherit master.kid can be written with their content
sitting directly inside the body tag. When the whole thing is generated
the master template will put them where they need to be. The only
problem with this approach is that any inherited template is *always*
going to be in this content-container div. For me that isn't an issue
as it is what I want, but its something to keep in mind.

-Adam

Reply all
Reply to author
Forward
0 new messages