ZPT Basics (part 3) (
Page 1 of 6 )
Now that you've got the skinny on variables and conditional statements, expand
your knowledge of the ZPT universe with this discussion of TAL loops, dynamically-generated
attributes and error handlers.If you've been following along, you now know a little more about ZPT, specifically
how to introduce decision-making logic into your templates by means of variables
and conditional tests. You also saw how all that theory can be put to practical
use by implementing a simple form processor for use on a Web page.
That's not all, though - ZPT also comes with a couple of low-key loop constructs
that allow you to iterate over sequences of both variables and objects. This ability
to create loops is not unique to ZPT - almost every programming language on the
planet allows you to do this - but as with DTML, ZPT includes some fairly interesting
twists to the traditional approach. Let's take a look.{mospagebreak title=Playing
The Numbers} Before we get started, let's get the jargon straight - what's a sequence
anyway?
As traditionally understood, a "sequence" is a series of items, usually connected
to each other by a logical thread. For example,
0, 1, 2, 3, 4, 5...
p, q, r, s, t...
huey, dewey, louie...
are all valid sequences
In the Zope context, the definition of a sequence can be scoped down a little
further - a sequence here is usually a set of objects. For example, if you create
a folder in Zope, the objects stored within that folder can be considered a sequence.
Or, if you retrieve a set of records from a database, the resultset returned can
also be considered a sequence of data items.
A sequence, however, is just one part of the jigsaw. In order to access the elements
that make up a sequence, you usually need a programming structure that will iterate,
or loop, through the sequence, processing each element in turn. This loop can
be combined with conditional statements to perform specific actions or execute
specific commands while processing the elements in a sequence.
In order to demonstrate this, let's create a sequence in Zope and write a loop
to process it. Fire up Zope, log into the Zope management interface, and create
an instance of the Page Template object. Name it "CountingDown"
and fill it with the following code:
<ul>
<li tal:repeat="countdown python:10,9,8,7,6,5,4,3,2,1,0">
<span
tal:replace="countdown" />
</li>
</ul>
When you view the output, here's what you'll see:
* 10
* 9
* 8
* 7
* 6
* 5
* 4
* 3
* 2
* 1
* 0
First, I've created a simple sequence by brute force - it contains numbers from
0 to 10, in reverse order. Note the use of the "python" keyword - this is used
to indicate that what follows is valid Python code.
The TAL "repeat" attribute is used to loop through the sequence, in a manner
similar to that used in the "for" loops PHP and Perl programmers are familiar
with. This "repeat" attribute takes two arguments - a variable name and an expression,
which must evaluate to a sequence. For each sequence iteration, the value of the
variable specified is set to the current element of the sequence. This variable
value is then used with the TAL "replace" attribute to print the contents of each
element in the sequence. From the output, it's obvious that this loop iterates
eleven times to display the elements in the sequence.