[Zope] Form Variable

Jonathan Hobbs toolkit at magma.ca
Wed Sep 22 15:21:21 EDT 2004


----- Original Message -----
From: "Asad Habib" <ahabib at engin.umich.edu>
> I have a form variable whose value is being passed to a DTML method upon
> form submission. In this DTML method, I use the form variable as input to
> a <dtml-in>. When this form variable is passed as a list(i.e. it contains
> multiple values), everything works fine. However, when this form variable
> is passed as a string(i.e. it contains a single value), <dtml-in>
> complains that it is being passed an input of the wrong type. Is there a
> way to detect whether this form variable is a list or string? Using the
> len function doesn't work because len works on both strings and lists.

You need the python 'type' statement, which unfortunately isn't available to
python scripts (its use is restricted). Therefore, you need a small external
method:


def typeof(self,a,b):
   if type(a) == type(b):
     return 1
   else:
     return 0


Then to use this method you can:

<dtml-call "REQUEST.set('a', [])">
<dtml-call "REQUEST.set('b', 'abc')">

<dtml-if "typeof(a,[])">
  a is a list<br>
</dtml-if>

<dtml-if "typeof(b,'')">
  b is a string<br>
</dtml-if>

Note: in the second dtml-if above, you are checking 'b' against an empty
string (two single quotes)

If you run the above dtml you will get:

a is a list
b is a string


HTH

Jonathan







More information about the Zope mailing list