[Zope] Problem refering to a subfolder object
Alexander Staubo
alex@mop.no
Sun, 11 Jul 1999 20:05:46 +0200
Something like this also works::
<!--#if "_.hasattr(images.global, section)"-->
<img src="images/global/<!--#var section-->">
<!--#else-->
<img src="images/global/default">
<!--#/if-->
The undocumented function _.hasattr() -- which afaik is just a wrapper
around the Python hasattr() function -- lets you determine if an object
contains a particular attribute. Since folder subobjects can be accessed
as Python attributes, this works.
Note that a potentially better way -- depending on your site
configuration and design -- might be::
<!--#if "_.hasattr(images.global, section)"-->
<img src="<!--#var "images.global[section].absolute_url()"-->">
<!--#else-->
<img src="<!--#var "images.global['default'].absolute_url()"-->">
<!--#/if-->
Because of acquisition, the URL "images/global/..." will work from
within anywhere in your site. That's good, but that does not help
browsers caching contents. In fact, by lazily accepting the wonders of
acquisitions you're working against the browser. To the web browser,
"/images/..." and "/mystuff/images/..." are not identical URLs, even if
they point to exactly the same object inside Zope. In the above snippet,
absolute_url() ensures that the OFS, not the acquired, URL is given, and
so it will always be given as "/images/global/[whatever]", which will
let browsers consistently cache the images.
Note how folder objects overload the subscripting operator. This means
that the syntax "Folder.<subobject>" is functionally equivalent to
"Folder['<subobject>']".
--
Alexander Staubo http://www.mop.no/~alex/
"`Ford, you're turning into a penguin. Stop it.'"
--Douglas Adams, _The Hitchhiker's Guide to the Galaxy_
>-----Original Message-----
>From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of Kam
>Cheung
>Sent: 11. juli 1999 18:42
>To: zope@zope.org
>Subject: [Zope] Problem refering to a subfolder object
>
>
>A very newbie question:
>
>How can I refer to a subfolder object? Basically, what I am
>trying to do is.
>given a string, I want to detect whether an image with the
>same name as the
>string exists or not in a subfolder, if it does, it display
>the object, if
>not, it will show a default image. Here is what I have tried,
>but doesn't
>work:
>
>Let's say, a document has a string called "section" and the
>value of it is
>"abc".
>
><!--#if name=images.global.[section]-->
> <IMG SRC="images/global/<!--#var section-->">
><!--#else-->
> <IMG SRC="images/global/default">
><!--#/if-->
>
>That doesn't work. I did find a way to do it by doing:
>
><!--#with images-->
> <!--#with global-->
> <!--#if name=section-->
> <IMG SRC="images/global/<!--#var section-->">
> <!--#else-->
> <IMG SRC="images/global/default">
> <!--#/if-->
> <!--#/with-->
><!--#/with-->
>
>But I think there must be an easier way to do that.
>
>
>
>
>Kam On Cheung
>
>_______________________________________________
>Zope maillist - Zope@zope.org
>http://www.zope.org/mailman/listinfo/zope
>
>(For developer-specific issues, use the companion list,
>zope-dev@zope.org - http://www.zope.org/mailman/listinfo/zope-dev )
>