[Zope] quote escaping in DTML

Thomas B. Passin tpassin@mitretek.org
Wed, 20 Feb 2002 16:00:28 -0500


[Milos Prudek]

> Dieter Maurer wrote:
> > Milos Prudek writes:
> >  > ... quoting ' ...
> > You can use triple quotes to enclose your string.
> > Such strings may contain single quotes unless they are at the start
> > or end of the string or happen to be three in a row:
> >
> >    '''This is a triple quoted string. It may contain '.'''
>
> You are right.
>
> But I found out that this advice can't be used in this particular case,
> because DTML rendering is impossible INSIDE the tag tag.
>
> In practical example:
>
> <dtml-var "tag(name=name, alt=name, onMouseOut='MM_swapImgRestore()',
>           onMouseOver='''MM_swapImage('<dtml-var fld>','','',0)''')">
>
> Here, <dtml-var fld> is not rendered but is reprinted verbatim. It's
> logical - you can't call <dtml-var> from <dtml-var>.... :-)
>
> Can you see any other way except reconstructing IMG SRC like I did in my

> post of Feb 6 2002 ?



But you don't need to put a <dtml> tag inside another one (and it doesn't
work).  Just refer to the variable or expression that you would are tempted
to put inside a <dtml> tag, and don't use the inner tag.

In this case, I assume that MM_swapImage() and MM_swapImgRestore() are
functions that the browser will call, not expressions for Zope to evaluate.
It seems the easiest and clearest to build the problem string separately.

Thus (I have added a few spaces so you can see where the double quotes are),

<dtml-let mouseover=" 'MM_swapImage(fld,' + _.chr(34)+_.chr(34)+','+
_.chr(34)+_.chr(34)+',0)' ">
<dtml-var "tag(name=name, alt=name, onMouseOut='MM_swapImgRestore()',
           onMouseOver=mouseover)">
</dtml-let>

I'm sure that line wrapped, but you get the idea.  _.chr(34) inserts a
double quote into the string. If you look at the result using <dtml-var
mouseover> you will see that the  string has been built correctly.

Cheers,

Tom P