[Zope] testing with dtml-if

Dylan Reinhardt zope at dylanreinhardt.com
Fri Sep 5 05:38:39 EDT 2003


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




More information about the Zope mailing list