[Zope] return structured result from Python scripts for page templates

Evan Simpson evan@4-am.com
Thu, 25 Jul 2002 11:04:20 -0500


Gabor Nagypal wrote:
> I would like to return a structured result (an "object") from a Python 
> script to use it conveniently in a page template.

As you've discovered, a dictionary will work.  It's not hard to create 
what you originally wanted, though.  Adding the following lines before 
the last line of Products/PythonScripts/standard.py will do it:

   from ZPublisher.HTTPRequest import record

   security.declarePublic('Object')
   class Object(record):
       _guarded_writes = 1

       def __setitem__(self, key, value):
           self.__dict__[str(key)] = value

This will allow you to write code like this:

   from Products.PythonScripts.standard import Object
   o = Object()
   o.x = 1
   o['spam!'] = 'Make Money Fast'
   return o

Cheers,

Evan @ 4-am