[Zope] Anything like a for loop in DTML?

Bill Randle billr@coinet.com
Mon, 12 Apr 1999 08:31:30 -0700


Phil,

I asked this same question awhile back. The answer is to use an External
Method. The Python equivalent is the "range" operator, but "range" is not
included in Zope due to possible problems with runaway memory usage if a
really big number was used. [In the example below you'll see that a max
limit is set. If it were a standard feature, where would one set the upper
limit?]

Copy this code to a file (say "looprange.py") and put it in
ZOPE_HOME/Extensions (you may need to create the directory).

# simple extension class to implement a counting loop
LoopRangeError = 'LoopRangeError' # Name an exception class.
loop_range_limit = 100            # Maximum allowable loop index.

def looprange(max):
    if max <= loop_range_limit:
	return range(max)
    else:
	raise LoopRangeError, max

Select External Method from the Add list and add this method (id="looprange",
method="looprange", file="looprange.py").

Now you can use it in your DTML like this:

<!--#in expr="looprange(ltblSize)"-->
... do some stuff for each loop iteration ...
<!--#/in-->

In my case "ltblSize" is a property of the DTML Document, but it could just as
well be a local variable in your REQUEST object.

	-Bill Randle
	Central Oregon Internet
	billr@coinet.com