[ZPT] Re: TALESPropertySheet or using TALES as a propertydefinition

Ulrich Eck ueck@net-labs.de
Wed, 8 Aug 2001 19:09:53 +0200


thank you very much !!

> (I'm CCing to ZPT in case this may help others):
> 

for any interested people, here what I made out of Evans tips:
--------------------------------------------------------------------------------------
import Globals
from Globals import Persistent
from Acquisition import aq_inner, aq_parent
from AccessControl import getSecurityManager, ClassSecurityInfo


from Products.PageTemplates.Expressions import getEngine, _SecureModuleImporter

SecureModuleImporter = _SecureModuleImporter()

class Expression (Persistent):
    method_name = ''
    _expr = None

    security = ClassSecurityInfo()

    def __init__(self, method_name):
        self.method_name = method_name
        e = getEngine()
        self._expr = e.compile(method_name)

    security.declarePrivate('validate')
    def validate(self, inst, parent, name, value, md):
        # Zope 2.3.x
        return getSecurityManager().validate(inst, parent, name, value)

    def __call__(self, context):
       e = getEngine()
       if self._expr is None:
             self._expr = e.compile(self.method_name)
       c = e.getContext(context)
       return c.evaluate(self._expr)

Globals.InitializeClass(Expression)


def exprNamespace(client,form, field=None, value=None, object=None):
    root = client.getPhysicalRoot()
    c = {'form': form,
         'field': field,
         'value': value,
         'here': object,
         'nothing': None,
         'options': {},
         'root': root,
         'request': getattr(root, 'REQUEST', None),
         'modules': SecureModuleImporter,
         }
    return c
--------------------------------------------------------------------------------------

use it like:
myExpr = Expression('string:this is a text')
ns = exprNamespace(...)
result = myExpr(ns)

thats it 

cheers
Ulrich Eck