[Zope] Strange CSS inclusion BUG

Li Dongfeng mavip1@inet.polyu.edu.hk
Wed, 20 Sep 2000 10:43:49 +0800


----- Original Message ----- 
From: "Dieter Maurer" <dieter@handshake.de>
To: "Li Dongfeng" <mavip1@inet.polyu.edu.hk>
Cc: "Zope" <zope@zope.org>
Sent: Wednesday, September 20, 2000 3:25 AM
Subject: Re: [Zope] Strange CSS inclusion BUG


> Li Dongfeng writes:
>  > To use CSS file, I put the following line in the head of HTML files:
>  > 
>  >     <dtml-if local_css>
>  >     <link href="<dtml-var "local_css.absolute_url()">"
>  >           rel="stylesheet" type="text/css">
>  >     </dtml-if>
>  > 
>  > Where "local_css" is a DTML document containing CSS style sheet.
>  > But the DTML generates the following error message:
>  > 
>  >     Error Type: AttributeError
>  >     Error Value: 'string' object has no attribute 'absolute_url'
>  > 
>  > If I strip the <dtml-if  protection, the code works OK.
>  > Why is the DTML document local_css changed into a string 
>  > after <dtml-if>?
> That is a dtml-if caveat!
> 
>   The dtml-if documentation says that dtml-if caches its
>   variable value to save time, in case the variable is
>   in fact a function that takes long to call.
> 
>   What happens in your case:
>     <dtml-if local_css> looks up "local_css" and because
>     it is callable, it gets called (i.e. rendered).
>     The result is a string. This string is bound to
>     "local_css" inside the <dtml-if>...</dtml-if>.
>     Therefore, "local_css.absolute_url" results in
>     the error you observed.
> 
> You can replace <dtml-if local_css> by
> <dtml-if "_.has_key(local_css)">.
> 

This really works! Even when the request is from a folder
below where local_css resides. This success sugests that
"_.has_key()" also use aquisition to search for objects.

> This will not be equivalent to "<dtml-if local_css>" but
> good enough for your purpose.
> 
> If you want to emulate it more faithfull, you could
> use:
> <dtml-if "_.has_key(local_css) and _['local_css']">
> 
> This is equivalent ot "<dtml-if local_css>" with the
> exception of the caching.
> 
> 
> Dieter