|
Instructional Technology Portfolio | Design and Development Tools | Lessons | Zope Lessons Login | Résumé | IT Portfolio | Home |
The DTML-IN tag is on of Zope's more powerful tags in that it allows you to not only iterate objects, but iterate them in a number of ways. As we saw in an earlier example, iterating a list if objectValues requires the DTML-IN tag to actually construct meaningful HTML markup.
More often than not, you simply need to reference the properties of the item that's being iterated over (i.e. title, date, size), but ocassionally, you need to reference the object itself, which is not something easily accomplished by default. The prefix tag allows you to "name" the item so that you can then reference easily within the loop. For example, the following code will print the name of the picture and then the picture itself:
<table> <dtml-in expr="objectValues('Image')" prefix="i"> <tr> <td align=right cellpadding=10><h3><dtml-var title><h3></td> <td><dtml-var i_item></td> </tr> </dtml-in> </table>
Notice that the item of the current iteration is simply the prefix appended with underscore followed by the word "item." This is the magic bullet giving you a handle on the item being iterated in the current loop. Through the power of Zope, the above emits HTML code looking like this:
<table> <tr> <td align=right cellpadding=10><h3>IT Eyes Revision 1<h3></td> <td><img src="http://www.codeconnoisseur.org/it/edit6190/journal/artifacts/resources/logos/iteyes1.gif" alt="IT Eyes Revision 1" height="142" width="296" border="0" /></td> </tr> <tr> <td align=right cellpadding=10><h3>IT Eyes Rev 2<h3></td> <td><img src="http://www.codeconnoisseur.org/it/edit6190/journal/artifacts/resources/logos/iteyes2.gif" alt="IT Eyes Rev 2" height="142" width="296" border="0" /></td> </tr> <tr> <td align=right cellpadding=10><h3>Simple IT Logo<h3></td> <td><img src="http://www.codeconnoisseur.org/it/edit6190/journal/artifacts/resources/logos/itlogo1.gif" alt="Simple IT Logo" height="143" width="296" border="0" /></td> </tr> </table>
Prefixes become decidedly important when you have nested loops and need to distinguish between the two items being iterated, especially when they have property names of the same identifiers. The following example demonstrates nested loops with two variables:
<dtml-in expr="('A', 'B', 'C')" prefix="letter"> <dtml-in expr="('0', '1', '2')" prefix="number"> &dtml-letter_item;&dtml-number_item; </dtml-in> </dtml-in>
| mwlang@cybrains.net | Guest | Login | Home |