Re: [Zope] How do I check for existence of a page?
You should not use a "dtml-in" to check for the existence of an object. You should directly check for the object: I many cases, you can use <dtml-if "_.hasattr(aq_explicit,'attrname')"> or <dtml-if "_.hasattr(object.aq_explicit,'attrname')"> to check whether the current object or "object", respectively, has attribute "attrname". In some rare cases, this can give you a "true" result, even though the object does not have an 'attrname'. A save way is an external method "baseHasAttr" defined by def baseHasAttr(object,attrname): obj= getattr(object,aq_base,object) return hasattr(obj,attrname) and a test through <dtml-if "baseHasAttr(this(),'attrname')> or <dtml-if "baseHasAttr(object,'attrname')> Dieter
On Wed, 28 Feb 2001, Dieter Maurer wrote:
You should not use a "dtml-in" to check for the existence of an object. You should directly check for the object:
I many cases, you can use
<dtml-if "_.hasattr(aq_explicit,'attrname')">
or
<dtml-if "_.hasattr(object.aq_explicit,'attrname')">
to check whether the current object or "object", respectively, has attribute "attrname".
In some rare cases, this can give you a "true" result, even though the object does not have an 'attrname'.
To help me understand Zope better (!), what are the 'rare cases' where this would give a false positive? Thanks for the help. -- Joel Burton <jburton@scw.org> Director of Information Systems, Support Center of Washington
Hi, Incidentally, to check for the existence of object_id in folder, I sometimes do something like this: <dtml-if "object_id in folder.objectIds()"> blah </dtml-if> Not the canonical way to do it, and I am completely unaware of what hidden implications I might face, so YMMV... The code is more legible, though. Cheers, J�r�me Loisel -- J�r�me Loisel L�vinux: GNU/Linux dans les communaut�s � L�vis
Joel Burton writes:
On Wed, 28 Feb 2001, Dieter Maurer wrote:
I many cases, you can use
<dtml-if "_.hasattr(aq_explicit,'attrname')">
or
<dtml-if "_.hasattr(object.aq_explicit,'attrname')">
to check whether the current object or "object", respectively, has attribute "attrname".
In some rare cases, this can give you a "true" result, even though the object does not have an 'attrname'.
To help me understand Zope better (!), what are the 'rare cases' where this would give a false positive? That is difficult to explain.
Search the (searchable) list archive for a post from me titled "unexpected acquisition behavior" (or something similar). Dieter
participants (3)
-
Dieter Maurer -
Joel Burton -
Jérôme Loisel