DTML Basics (part 4) - A Polite Request (
Page 3 of 9 )
Take a look at this next example,
which demonstrates how the <dtml-with> tag can be used to work around the problems
encountered in the previous example.
<b>
<dtml-with REQUEST only>
<dtml-if id>The value of "id" is <dtml-var
id><br></dtml-if> <dtml-if
icon>The value of "icon" is <dtml-var icon><br></dtml-if>
<dtml-if
me>The value of "me" is <dtml-var me><br></dtml-if> </dtml-with>
</b>
Now, take a look at the output:
The value of "id" is 1234
The value of "icon" is folder
The value of "me" is
John
How did this work? The <dtml-with> tag - it is used to push a object to the
top of the DTML namespace. As a result, Zope will look for all the variables in
this object first.
<dtml-with REQUEST only>
The above statement pushes the REQUEST object to the top of the namespace stack.
This object consists of the CGI environment variables, cookies and form data sent
using the GET and POST methods.
For a quick glimpse into this object, create a new DTML Document with the following
code.
<dtml-var REQUEST>
And when you view the output of this script, you'll see exactly what the REQUEST
object contains.
The additional "only" attribute in the call to <dtml-with> tells Zope that
it should not look beyond the REQUEST object for these values. Why bother with
this extra tag? Consider what will happen if you don't pass a value for the "id"
parameter - once Zope checks the REQUEST object and fails to find a value for
this variable, it will proceed down the namespace stack to see if a value exists
anywhere else. And we know it does - which could lead to unexpected results. The
"only" attribute is the only (pardon the pun) way to ensure that this does not
happen.
<dtml-if id>The value of "id" is <dtml-var id><br></dtml-if> <dtml-if
icon>The
value of "icon" is <dtml-var icon><br></dtml-if> <dtml-if
me>The value
of "me" is <dtml-var me><br></dtml-if>
The rest of the code checks to see whether the named variable exists in the REQUEST
object, and only displays the corresponding line of output if it does. Again,
this is a good way to avoid the unexpected, and to prevent bugs creeping in.