Extending templates and overriding values

0 views
Skip to first unread message

Jeff Grimmett

unread,
Oct 28, 2005, 10:37:07 PM10/28/05
to turbo...@googlegroups.com
Hi all,

I'm - gradually! - getting my head wrapped around Kid templates but have hit a puzzling behavior.

Here's what I've got:

master.kid:

=== snip ===
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?python import sitetemplate ?>
<?python page_title = "" ?>

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#" py:extends="sitetemplate" lang="en">

<head py:match="item.tag=='{http://www.w3.org/1999/xhtml}head'">
    <meta content="text/html; charset=UTF-8" http-equiv="content-type" py:replace="''"/>

    <link rel="stylesheet" href="css/base.css" type="text/css" />
    <link rel="home" title="Home" href="http://www.grimmlabs.com/ " />
    <title>${page_title}</title>

</head>

... etc ...
=== snip ===

And home.kid:

=== snip ===
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml " xmlns:py="http://purl.org/kid/ns#" py:extends="'master.kid'" lang="en">

<?python page_title = "Home Page" ?>

<head></head>

... etc ...
=== snip ===

What I'm trying to do is make it so that I don't have to re-create my <head> element in every template that I create, since that will be mostly consistent for all pages.  HOWEVER, the <title> will have to be different for every page.

Since TITLE is embedded in HEAD I can't substitute it in the 'home' template without providing my own 'head' element for each template.  So what I thought I could do is use the python variable 'page_title' in the 'home' template and that would be substituted into the included header.

That's not happening. If I use what's above, the <title> element is empty. If I don't define it as "" in the master template, then an exception is thrown.

BUT

If I do something like this:

    @turbogears.expose(html="grimmlabs.templates.home")
    def index(self):
        return dict(page_title="Home Page",)

then it gets filled in properly.

I can LIVE with the way it's working, but I would PREFER to keep as much in the template itself and save myself a bunch of trivial stuff in the code itself.

Any ideas?

(note: it may be obvious to you. Assume it's not to me. 'cause it ain't.)

--
"Things fall apart. The Center cannot hold."
                  - Life as a QA geek, in a nutshell.

Best,

    Jeff

Kevin Dangoor

unread,
Oct 28, 2005, 11:15:55 PM10/28/05
to turbo...@googlegroups.com
Hi Jeff,

There are two ways to do this (that immediately come to mind... there
may be more, of course).

In master.kid's <head>:

<title py:def="pagetitle()">Sample Title</title>
<title py:replace="${pagetitle}" />

(aside: there's a py:slot command that I saw a ticket for. I don't
remember if it was implemented, but it essentially does what I just
did: a def with an immediate inclusion in the template)

In home.kid's <head>:
<title py:def="pagetitle()">The Real Title</title>


The rule of thumb that I like to keep in mind when extending a
template is that the reason to extend is match templates and defs.
Don't count on any other sharing between master and child templates.

Now for the other solution (which is the nicer one, since you're
already using a match template):

In master.kid's <head>:
<meta py:replace="item[:]"/>

In home.kid's <head>:
<title>The Real Title</title>


The meta tag gets completely thrown out and replaced with the contents
of the child template's <head> (item[:]).

Kevin
--
Kevin Dangoor
Author of the Zesty News RSS newsreader

email: k...@blazingthings.com
company: http://www.BlazingThings.com
blog: http://www.BlueSkyOnMars.com

Jeff Grimmett

unread,
Oct 30, 2005, 1:00:31 AM10/30/05
to turbo...@googlegroups.com
On 10/28/05, Kevin Dangoor <dan...@gmail.com> wrote:

In master.kid's <head>:

<title py:def="pagetitle()">Sample Title</title>
<title py:replace="${pagetitle}" />

This more or less worked except this as given raised a syntax error. I changed the second line to:

    <title py:replace="pagetitle()" />

and it worked.
 

Now for the other solution (which is the nicer one, since you're
already using a match template):

In master.kid's <head>:
<meta py:replace="item[:]"/>

In home.kid's <head>:
<title>The Real Title</title>


The meta tag gets completely thrown out and replaced with the contents of the child template's <head> (item[:]).

I haven't tried this yet but I like the looks of it better because it fits in more elegantly with plans I have down the road. I'll give it a whirl and let you know of any issues if I find them.

Oh, and regarding the comment about "not relying on things being shared between the two", I was actually expecting python class-like behavior. If I use a similar idiom with a calss and then subclass it, then the latter substitution would work. I inferred that similar behavior was taking place here. Obviously, it LOOKS LIKE but IS NOT the same thing. Just wanted to explain where my mind was going with that :-)

Thanks again!

Jeff Grimmett

unread,
Oct 30, 2005, 1:16:18 AM10/30/05
to turbo...@googlegroups.com
On 10/28/05, Kevin Dangoor <dan...@gmail.com> wrote:
Now for the other solution (which is the nicer one, since you're
already using a match template):

In master.kid's <head>:
<meta py:replace="item[:]"/>

In home.kid's <head>:
<title>The Real Title</title>

The meta tag gets completely thrown out and replaced with the contentsof the child template's <head> (item[:]).

Yep, that worked great with no issues whatsoever, and is far nicer, as you said. Thanks!  That also helps a bit with understanding that item[:] construct. Didn't quite grasp what was going on with that before.

Reply all
Reply to author
Forward
0 new messages