Doubt about the ZPT example in Using Zope Page Templates
Hi, I'm sory about posting this comment here, but although I log into zope.org, I can't add a comment here: http://www.zope.org/Documentation/Books/ZopeBook/current/ZPT.stx/commentForm... The thing is that I was trying to figure out how one could add a default title when a document doesn't have one. If you run the previous example (see the link above), you'll see an ugly cell without borders when a title wasn't provided. So, I discovered that it could solved replacing the line where the titles are written by this one: <td tal:content="python:item.title or default">No Title</td> I still don't understand why this sintax doesn't work: <td tal:content="python:item/title or default">No Title</td> It returns: Error Type: NameError Error Value: global name 'title' is not defined but in fact, title is an attribute of item. Any comments? Thanks in advanced, Josef
On Fri, 27 Jun 2003 12:11:56 +0200 GMT Josef Meile asked the Zope mailinglist about the following:
I still don't understand why this sintax doesn't work: <td tal:content="python:item/title or default">No Title</td>
It returns: Error Type: NameError Error Value: global name 'title' is not defined
but in fact, title is an attribute of item.
item/title is not valid python syntax. using "python:item.title or default" or even cleaner, a tales-statement: "item/title | default" just might help.. :) -- Geir Bækholt
Geir Bækholt wrote:
using "python:item.title or default" or even cleaner, a tales-statement: "item/title | default" just might help..
Hmmm, yeah I wonder why title_or_id() was removed from CMF products? instead we have to use: tal:define="title_or_id python:test( item.Title(), item.Title(), item.getId()) And that sucks! as it is so often used. Guess I will have to put a "util_title(item)" script in my skin folder ## Script (Python) "util_title" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters=item, default=None ##title=Returns title, default of id ## # Returns title, default value, or title for item, in that order. try: return item.Title() except: if default: return default return item.getId() regards Max M
Max M wrote:
try: return item.Title() except: if default: return default return item.getId()
Oh yeah. Like if that will work: title = item.Title().strip() # sometimes a title is a space if title: return title if default: return default return item.getId() Max
Josef Meile wrote at 2003-6-27 12:11 +0200:
... So, I discovered that it could solved replacing the line where the titles are written by this one:
<td tal:content="python:item.title or default">No Title</td>
I still don't understand why this sintax doesn't work: <td tal:content="python:item/title or default">No Title</td>
Because in Python "/" is the division operator. Therefore "item/title" in Python is fundamentally different from "item/title" as path expression. I usually use in such cases <XXXX tal:define="title item/title;" tal:content="python: title or default">....</XXXX> Dieter
participants (4)
-
Dieter Maurer -
Geir Bækholt -
Josef Meile -
Max M