[Zope] Re: How to do...[Rendering zpt on the fly]

Josef Meile jmeile at hotmail.com
Fri Oct 27 17:17:39 EDT 2006


Hi Garito,

I saw in another thread you still need this. If I'm not wrong what you 
are pretending is to somehow render zpt on the fly. So, here is my idea:
> I would like to write things like:
> 
> object.property = 'here/anotherproperty'
> object.anotherproperty = 'Barcelona'
> 
> and ask it like:
> 
> print object.property -> 'Barcelona'
> 
> or
> 
> <tal:city tal:replace='here/property' /> -> 'Barcelona'
> 
> Is this possible?
I think you could perhaps create a new class to store the tal expression 
attributes, let's say you called it ExpressionAttribute. This class 
must be callable and be able to evaluate tal expressions. This is a 
small sketch of how I think it should look like:

from Products.PageTemplates.ZopeTemplate import ZopeTemplate

class ExpressionAttribute:
   #Here I also assume that you only enter the expressions
   #without html and the tal part, like: 'here/property'
   #Please also noticed that all sintax errors in your zpt
   #code must be catched somewhere
   def __init__(self, id, talExpr):
     self.talExpr = '<span tal:replace="%s"/>' % talExpr

     #I'm not sure about this, but I think it will create a Template on
     #the fly.
     self.zpt = ZopePageTemplate(id, text = self.talExpr)

   #Note: I really don't know if you have to pass other parameters to
   #pt_render, ie: the container of your property. You will have to test
   #this.
   def __call__(self):
     return self.zpt.pt_render()

Then, in the __init__ of your zope product you will need to do things 
like this:

class YourProd(...):
   def __init__(self,prop1,prop2,prop3,...):
     #This is a normal property
     self.prop1 = prop1

     #This two are tal expression properties
     self.prop2 = ExpressionAttribute(prop1)
     self.prop3 = ExpressionAttribute(prop1)

Then, I guess you can call it as:
yourProdObj.prop3 or in tal: here/prop3

I'm not sure about what I wrote and if this is what you are looking for. 
I just hope it helps.

Regards
Josef


More information about the Zope mailing list