You are not logged in Log in Join
You are here: Home » Members » Zope Stuff » Looping in DTML

Log in
Name

Password

 

Looping in DTML

Looping in DTML

This uses the Zope 2 syntax of <dtml-in "..."> ... </dtml-in>. The Zope 1.10 syntax will also work (<!--#in "..."--> .. <!--#/in-->) The DTML loop

Example using pre-cooked values

<dtml-in "16,21,3,49">
item# <dtml-var sequence-index>,<dtml-var sequence-number> = <dtml-var sequence-item>
</dtml-in>

and it's output

item#�0,1 = 16
item#�1,2 = 21
item#�2,3 = 3
item#�3,4 = 49

Example using ranged values (only available in Zope 2)

You can use the _.range() function to specify a range of values to iterate over (you're limited to 1000 items). _.range(10) will iterate over the values 0..9. _.range() works in the same way as the python function range(), in that you can also specify start, stop and increment values. e.g.
<dtml-in "_.range(15, 33, 4)">
item# <dtml-var sequence-index>,<dtml-var sequence-number> = <dtml-var sequence-item>
</dtml-in>
and it's output

item#�0,1 = 15
item#�1,2 = 19
item#�2,3 = 23
item#�3,4 = 27
item#�4,5 = 31

Some Real Work - a database lookup

Assume you have a ZSQL method called get_users in your acquisition tree that doesn't take a parameter. Something like "SELECT username, password, roles, domains FROM users". Then this code will show all the elements in the table;
<dtml-in "get_users()">
Username:<dtml-var username>, Password:<dtml-var password>, Roles:<dtml-var role>, Domains:<dtml-var domains><br>
</dtml-in>
If you have another ZSQL method get_user_by_username that uses a parameter, username, with the following code: SELECT username, password, roles, domains WHERE username = <dtml-sqlvar username type=string> then this will get the table rows with a particular username
<dtml-in "get_user_by_username(username='tone')">
Username:<dtml-var username>, Password:<dtml-var password>, Roles:<dtml-var roles>, Domains:<dtml-var domains><br>
</dtml-in>