Does anyone know the syntax to use with dtml-if when i want to do something like this: <dtml-if <dtml-var w>==1> do stuff </dtml-if> ? thanks garry
On Thu, Sep 04, 2003 at 09:24:53PM +0100, garry saddington wrote:
Does anyone know the syntax to use with dtml-if when i want to do something like this:
<dtml-if <dtml-var w>==1>
do stuff </dtml-if> ?
How is that different from <dtml-if expr="w == 1"> ... or possibly <dtml-if expr="`w` == 1"> ... Dustin -- Dustin Mitchell dustin@ywlcs.org/djmitche@alumni.uchicago.edu http://people.cs.uchicago.edu/~dustin/ PGP Key: http://people.cs.uchicago.edu/~dustin/pubkey.txt
Does anyone know the syntax to use with dtml-if when i want to do something like this:
<dtml-if <dtml-var w>==1>
do stuff </dtml-if> ?
This means, well, nothing. It's syntactically invalid. DTML is not a macro language, but an interpreted one, and the use of DTML inside a DTML tag is at best confusing and at worst a syntax error, and in no case is such a tag evaluated. The contents of a dtml-if can be either a name (without quotes) or a python statement (inside quotes, perhaps preceded by 'expr='.)
In this case, you are asking dtml-if about the truth value of an object named '<dtml-var w>==1', which is not a valid name. Or, at least, since I haven't tried, it shouldn't be.
How is that different from
<dtml-if expr="w == 1"> ...
This is asking about the truth value of the Python expression 'w==1', which will be true iff the contents of the first thing in the DTML namespace stack (the request, parameters, properties, acquisition, dtml loops, defines, et al) with the name of 'w' is equal within the Python definition to the integer 1.
or possibly
<dtml-if expr="`w` == 1"> ...
This is asking about the truth value of the Python expression " 'w'==1 ", which means "the character 'w' is equal to the integer 1." This will never be true, since 'w' is not 1. --jcc -- "Code generators follow the 80/20 rule. They solve most of the problems, but not all of the problems. There are always features and edge cases that will need hand-coding. Even if code generation could build 100 percent of the application, there will still be an endless supply of boring meetings about feature design." (http://www.devx.com/java/editorial/15511)
On Thu, Sep 04, 2003 at 08:08:39PM -0500, J Cameron Cooper wrote:
or possibly
<dtml-if expr="`w` == 1"> ...
This is asking about the truth value of the Python expression " 'w'==1 ", which means "the character 'w' is equal to the integer 1." This will never be true, since 'w' is not 1.
Your explaination was spot-on up until this one. `w` (backticks) is the string representation of the value of w. 'w' (single quotes) is the character 'w'. Perhaps your mailreader confuses the rendering of such punctuation. Anyway, the purpose of the above dtml-if tag is to more closely match the effect one might expect of a (admittedly illegal) nested dtml-var, as dtml-var stringifies its argument. In the end, you're right, though, because no string will ever equal 1.* Dustin * Unless you're in Perl-land or PHP-land. If you are, I offer my condolances and hope you are allowed to return soon. :-) -- Dustin Mitchell dustin@ywlcs.org/djmitche@alumni.uchicago.edu http://people.cs.uchicago.edu/~dustin/ PGP Key: http://people.cs.uchicago.edu/~dustin/pubkey.txt
What's confusing about this is that single and double quotes have the same meaning in Python... but this is not so in DTML. Not really. When you use a DTML tag like: <dtml-if "some expression"> or <dtml-var "some expression"> The "some expression" that's in double quotes is a Python expression. A Python expression is a bit of Python code that evaluates to a value. It can be simple or complex. The important thing is that it returns a value. The double quotes are not part of the expression, they merely show where the expression begins and ends. Single quotes will not work for this purpose. Single quotes *will* work if you need quote marks *within* an expression, eg: <dtml-if "my_var == 'w'"> This tests if my_var is equal to the string 'w'. If you wanted to see if my_var is equal to the value of a variable called w, you'd say: <dtml-if "my_var == w"> HTH, Dylan On Thu, 2003-09-04 at 18:08, J Cameron Cooper wrote:
Does anyone know the syntax to use with dtml-if when i want to do something like this:
<dtml-if <dtml-var w>==1>
do stuff </dtml-if> ?
This means, well, nothing. It's syntactically invalid. DTML is not a macro language, but an interpreted one, and the use of DTML inside a DTML tag is at best confusing and at worst a syntax error, and in no case is such a tag evaluated. The contents of a dtml-if can be either a name (without quotes) or a python statement (inside quotes, perhaps preceded by 'expr='.)
In this case, you are asking dtml-if about the truth value of an object named '<dtml-var w>==1', which is not a valid name. Or, at least, since I haven't tried, it shouldn't be.
How is that different from
<dtml-if expr="w == 1"> ...
This is asking about the truth value of the Python expression 'w==1', which will be true iff the contents of the first thing in the DTML namespace stack (the request, parameters, properties, acquisition, dtml loops, defines, et al) with the name of 'w' is equal within the Python definition to the integer 1.
or possibly
<dtml-if expr="`w` == 1"> ...
This is asking about the truth value of the Python expression " 'w'==1 ", which means "the character 'w' is equal to the integer 1." This will never be true, since 'w' is not 1.
--jcc
<dtml-if "w==1"> do stuffs </dtml-if w==1> if "w" was passed as a string then put single quotes around '1' or convert it to an integer. <dtml-if "_.int(w)==1" > cheers, Mike On Thursday 04 September 2003 02:24 pm, garry saddington wrote:
Does anyone know the syntax to use with dtml-if when i want to do something like this:
<dtml-if <dtml-var w>==1>
do stuff </dtml-if> ?
thanks garry
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
garry saddington wrote:
Does anyone know the syntax to use with dtml-if when i want to do something like this:
<dtml-if <dtml-var w>==1>
do stuff </dtml-if> ?
<dtml-if "_.getitem('w')==1"> do stuff </dtml-if> ? cheers, Chris PS: Mvoe to ZPT and save yoruself long term pain...
On Friday 05 September 2003 14:38, Chris Withers wrote:
garry saddington wrote:
Does anyone know the syntax to use with dtml-if when i want to do something like this:
<dtml-if <dtml-var w>==1>
do stuff </dtml-if> ?
<dtml-if "_.getitem('w')==1"> do stuff </dtml-if> ?
cheers,
Chris
Thanks for your help, but let me run this by you: <dtml-if w> does what it should do but what you suggest does not work nor do any of the other combinations i have tried. I can demostrate that w=1 by displaying <dtml-var w>. Any further ideas regards garry
On Fri, Sep 05, 2003 at 06:52:14PM +0100, garry saddington wrote:
<dtml-if w> does what it should do but what you suggest does not work nor do any of the other combinations i have tried. I can demostrate that w=1 by displaying <dtml-var w>.
ah, but do you know whether it's 1 or "1"? If you want to treat them as equivalent, convert w to an integer. <dtml-if "int(_.getitem(w))==1"> ... -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's LUDICROUS SPUTNIK FROM PLANET POOTOOT! (random hero from isometric.spaceninja.com)
On Fri, 2003-09-05 at 10:52, garry saddington wrote:
<dtml-if w> does what it should do
If "what it should do" means "testing the existence *and* truth of w". If w is *any* non-False value such as 1, 2, 'hello world', or ['a','b','c'] this test will evaluate True. If w is 0, [], None, or doesn't exist, it will evaluate False. If those results are what you want, cool.
but what you suggest does not work nor do any of the other combinations i have tried. I can demostrate that w=1 by displaying <dtml-var w>.
Or maybe what you've demonstrated is that w is equal to '1', which is going to render the same on-screen but is not the same data. If w is a variable you're grabbing from a form, that's the case almost for certain. You're far better off writing your tests for the *exact* condition you're testing, rather than using a broad existence/truth test and assuming the rest. If the test "w==1" does not return True, then w is not equal to 1. Maybe it's equal to something that looks like 1, but this is not the same thing. HTH, Dylan
On Friday 05 September 2003 19:21, Dylan Reinhardt wrote:
On Fri, 2003-09-05 at 10:52, garry saddington wrote:
<dtml-if w> does what it should do
If "what it should do" means "testing the existence *and* truth of w".
If w is *any* non-False value such as 1, 2, 'hello world', or ['a','b','c'] this test will evaluate True. If w is 0, [], None, or doesn't exist, it will evaluate False. If those results are what you want, cool.
but what you suggest does not work nor do any of the other combinations i have tried. I can demostrate that w=1 by displaying <dtml-var w>.
Or maybe what you've demonstrated is that w is equal to '1', which is going to render the same on-screen but is not the same data. If w is a variable you're grabbing from a form, that's the case almost for certain.
You're far better off writing your tests for the *exact* condition you're testing, rather than using a broad existence/truth test and assuming the rest. If the test "w==1" does not return True, then w is not equal to 1. Maybe it's equal to something that looks like 1, but this is not the same thing.
HTH,
Dylan Thanks for all your help, it is great to feel un-isolated! I have inspected the Request object and the 1 is indeed a '1' so now it works. best regards garry
participants (7)
-
Chris Withers -
Dustin Mitchell -
Dylan Reinhardt -
garry saddington -
J Cameron Cooper -
Mike Doanh Tran -
Paul Winkler