[ZPT] Re: Dynamic title for template?

Tim Moore tmoore@tembel.org
27 Apr 2001 17:04:17 +0000


Well, in case anybody's curious, here's what I ended up doing:

In the standard template, I define a local variable title at the top
of the file:

<html metal:define-macro="master"
      tal:define="title template/findTitle">

then I use that variable wherever I want to insert the title

findTitle is a Python script that I added to the top folder of the
site.  It starts at the template and recursively goes up the ZODB
hierarchy looking for the first non-empty title:

title = context.title

# The following allows me to put "title" python scripts into my
# ZClasses to dynamically generate the title.
if callable(title):
  title = title()

if title == "":
  title = context.getParentNode().findTitle()

return title

This seems to work OK for both static string titles and dynamic script
titles. :-)

"Evan Simpson" <evan@digicool.com> writes:

> From: "Tim Moore" <tmoore@tembel.org>
> > I have a standard macro that's used by all my templates to provide
> > navigation links, basic layout, etc.  It contains the title tag.
> >
> > What I'd really like is for the title tag to be set to the title
> > of the template if it has one, or the title of the container if
> > the template's own title is blank.  Is it possible to do this as a
> > simple path reference, rather than having to resort to
> > conditionals and python trickery?
> 
> Ah, I see.  Right now, the best you can do is:
> 
> <title tal:content="python:template.title or container.title">Title</title>
> 
> In 1.2, you will be able to write:
> 
> <title tal:content="(if) template/title"><span
> tal:replace="container/title">Title</span></title>

-- 
Tim Moore