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. Now, take a look at the output: 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. 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. 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. 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.
blog comments powered by Disqus |