[Zope] Creating sequences

Jeff K. Hoffman jeff.hoffman@goingv.com
Wed, 15 Mar 2000 15:37:59 -0500 (EST)


On Wed, 15 Mar 2000, Ken Kinder wrote:

> I know that you can use the dtml-in tag to iterate over a sequence, but
> I want to know how to create a sequence? If, for example, in Python I
> had:
> 
> foo = [1,2,3]
> 
> What would I have to do to get a sequence like that accessible from
> Zope? I didn't see list in the list of object types I can add.

You have a few (untested) choices:

1) Put the following in a DTML Document or Method:

     <dtml-call expr="REQUEST.set('first_sequence', [1, 2, 3])">
     First:<br>
     <dtml-in first_sequence>
       <dtml-var sequence-index>: <dtml-var sequence-item>
     </dtml-in>

     <dtml-let second_sequence="[1, 2, 3]">
       Second:<br>
       <dtml-in second_sequence>
         <dtml-var sequence-index>: <dtml-var sequence-item>
       </dtml-in>
     </dtml-let>

2) Create a DTML Document. Add a "my_sequence" property to the document of
   type "Tokens". Give it the value "1 2 3". Put this as the document
   source:

     <dtml-in my_sequence>
       <dtml-var sequence-index>: <dtml-var sequence-item>
     </dtml-in>

3) Create a DTML Document. Add a "my_sequence" property to the document of
   type "Lines". Give it the value:

     1
     2
     3

   and put the following as the document source:

     <dtml-in my_sequence>
       <dtml-var sequence-index>: <dtml-var sequence-item>
     </dtml-in>

4) We could make a Python base class
   (lib/python/Products/MyClassBase/MyClassBase.py):

     class MyClassBase:
       def __init__(self):
         self.my_sequence = [1, 2, 3]

   and create a file (lib/python/Products/MyBase/__init__.py):

     from MyClassBase import MyClassBase

     def initialize(context):
       context.registerBaseClass(MyClassBase, 'MyClassBase')

   Now restart your Zope, go to the Control Panel, and Add a new product
   called "MyClass". Inside this product, create a ZClass called
   "MyClass". When creating the ZClass, select MyClassBase as a base
   class. In this ZClass, on the Methods tab, create a DTML Method
   (say, index_html) with the following source:

     <dtml-in my_sequence>
       <dtml-var sequence-index>: <dtml-var sequence-item>
     </dtml-in>

   Then, go to your root, drop an instance of MyClass with an id of
   "MyInstance". Now, navigate to http://your.server.com/MyInstance.
   Voila.

I probably missed two or three other ways to to it. But, I hope one of
these is what you're looking for.

> Ken Kinder

--Jeff

---
Jeff K. Hoffman                                         704.849.0731 x108
Chief Technology Officer                                mailto:jeff@goingv.com
Going Virtual, L.L.C.                                   http://www.goingv.com/