Theexample you've just seen was very rudimentary. To really add somepunch, you need to know how to construct what the geeks call aconditional statement. And the very basis of a conditional statement iscomparison - for example, "if this is equal to that, do thus and such". Since Zope is built around Python, and Python comes with a bunch ofuseful operators designed specifically for use in conditionalstatements, you can use Python operators to build conditional statementsin your DTML. Here's alist: These comparison operators can be used for both strings and numbers. Apositive result returns true (1), while a negative result returns false(0). Here's an example, a DTML Method named "access", which illustrates howthese can be used: Now, if you view this example in your browser, you'll see an ugly Zopeerror - this is because Zope cannot locate the variable "name", despiteits best attempts to acquire it from the global namespace. Therefore, inorder to see this script work properly, you need to first make sure thata variable named "name" actually exists. You can do this by adding thisvariable to the REQUEST object, via the URL GET method; just call theDTML script using a URL similar to this one: http://localhost:8080/DTML%20Basics/access?name=Joe Here's the output:. Flip it around to see what it looks like when the variable named "name"holds the value "neo". Here's the URL to use, http://localhost:8080/DTML%20Basics/access?name=neo and here's the output: In this case, since the conditional expression in the "if" statementevaluates to true, the output displayed by the script will be different. There's one more thing that needs to be explained in the code above -the "expr" keyword. Normally, you can directly test whether or not avariable exists using the <dtml-var> or the <dtml-if> tags (as I've donein my very first example). However, if you want to do anything morecomplicated - for example, evaluate a DTML expression such as or - you need to first tell the DTML interpreter that you're talking aboutan expression and not a variable. And that's where "expr" comes in - itlets Zope know that it's dealing with a conditional expression, and notjust a regular variable.
blog comments powered by Disqus |