The example 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 ZPT. Here's alist: These comparison operators can be used for both strings and numbers. Apositive result returns true, while a negative result returns false. Here's an example which illustrates how these operators can be combinedwith the "condition" attribute discussed on the previous page to set upmultiple decision branches within a template. Here's what the output looks like: Now, try changing the value of the "name" variable in the first line ofthe template, and look what happens: Nothing too complicated here. A quick glance at the code will show youthat I've used the TAL "condition" attribute twice, in two differenttests. The first time around, the first conditional test will evaluateto true and the second will evaluate to false, because the value of thevariable is set to "neo"; consequently, the contents of the first <span>block will be displayed, while the contents of the second will beomitted. The second time around, since the value of the variable is no longer"neo", the second test will evaluate to true and the contents of thesecond <span> block will be displayed. I know what you're thinking - this would have been much easier toimplement with an "if-else" conditional statement. Unfortunately, TALdoesn't have one of those yet; the only way to obtain that functionalityis to simulate it via multiple "condition" attributes, as in the exampleabove.
blog comments powered by Disqus |