ZPT Basics (part 3) - Members Only (Page 3 of 6 )
You can also use the "repeat" attribute to hook your template up to a database, and dynamically generate output based on an SQL resultset. Consider the following example, which demonstrates how the "repeat" attribute can be used to iterate through a MySQL resultset and dynamically build a table containing elements of that resultset.
<table cellpadding="5" cellspacing="0" border="1" width="250">
<tr bgcolor="#CFCFCF">
<td align="left"><b>User Details</b></td>
</tr>
<tr tal:repeat="memberlist
here/SelectMembers">
<td align="left"><font size="2"><b><span
tal:replace="memberlist/fname">First
name here</span><span
tal:replace="memberlist/lname">Last name here</span></b></font>
<br>
<font size="1"><i tal:content="memberlist/tel">Telephone number
here</i>
<br> <span tal:replace="memberlist/email">Email address
here</span></font>
</tr> </table>
In this example, SelectMembers is a ZSQL method used to retrieve a list of members
from a database SELECT * FROM members as a sequence (for the uninitiated, a ZSQL Method is a special Zope object that allows you to communicate with a database). A ZPT loop iterates through it and prints the data as an HTML table.
Here's what the output looks like:
This example uses the "repeat" TAL attribute to loop over the result set returned by the SelectMembers ZSQL Method.
<tr tal:repeat="memberlist here/SelectMembers">
The TALES expression - "here/SelectMembers" - executes the ZSQL method. The rest
of the code is good ol' HTML to make the page look pretty.
More information on the "repeat" attribute can be found at
http://www.zope.org/Wikis/DevSite/Projects/ZPT/RepeatVariableNext: Sticks And Stones >>
More Zope Articles
More By Harish Kamath, (c) Melonfire