Is it possible to escape quotes in DTML? There are situations where single and double quotes pile up in a manner that makes expressions invalid. Example: I need to generate the following programatically: <img src="/images/on/usability" width="82" height="20" border="0" name="usability" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('usability','','/images/on/usability',0)"> First try is working but not very readable: <img src=<dtml-var "images.on.usability" url> <dtml-with "images.on.usability"> height=<dtml-var height> width=<dtml-var width> border=0 name=<dtml-var title_or_id> alt=<dtml-var title_or_id> onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('usability','','/images/off/usability',0)" </dtml-with>> Second try, using image tag tag: <dtml-with "images.on.usability"> <dtml-let name=title_or_id> <dtml-var "tag(name=name, alt=name, onMouseOut='MM_swapImgRestore()', onMouseOver='MM_swapImage('usability','','/images/off/usability',0)')"> </dtml-let> </dtml-with> This does not work, because single quote levels fight each other. I tried to substitute value of onMouseOver using dtml-let="MM_swapImage('usability','','/images/off/usability',0)", but that's not working either, because Zope tries to evaluate MM_swapImage which is impossible to do because it's JavaScript code :-) So elegance goes overboard and I stick with the first try. Is there a cleaner way to do this? -- Milos Prudek
[Milos Prudek]
Is it possible to escape quotes in DTML?
There are situations where single and double quotes pile up in a manner that makes expressions invalid.
Yes, escape an apostrophe with a backslash (\') and a double quote with _.chr(34) Cheers, Tom P
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 '.''' Dieter
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 ? -- Milos Prudek
[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
I wrote:
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>
Another way that is probably more readable to build the string is to interpolate string pieces using the Python %s syntax: <dtml-let mouseover=" 'MM_swapImage(fld,%s%s,%s%s,0)' % (_.chr(34),_.chr(34),_.ch(34),_.chr(34))"> Cheers, Tom P
participants (3)
-
Dieter Maurer -
Milos Prudek -
Thomas B. Passin