[Zope] adding dtml/zpt methods to product classes

Pascal Peregrina Pperegrina at Lastminute.com
Fri Jan 28 02:33:28 EST 2005


Why don't you simply define the DTML/ZPT objects attributes outside any
class method ?

class HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item): 
	my_method = Globals.HTMLFile("dtml/my_method",globals())
	my_new_method = Globals.HTMLFile("dtml/my_new_method",globals()) 
	...

This is what I do, and any new DTML/ZPT object I define that way gets added
to any existing instance of this class type when I restart Zope (or when
refresh my product).

Pascal


-----Message d'origine-----
De : zope-bounces+pperegrina=lastminute.com at zope.org
[mailto:zope-bounces+pperegrina=lastminute.com at zope.org]De la part de Keith
Alperin
Envoyé : vendredi 28 janvier 2005 04:59
À : zope at zope.org
Objet : [Zope] adding dtml/zpt methods to product classes


Greetings Zopistas! 


I have been a Zope tinkerer for a few years and have never been able to
adequately solve this problem: When I create a Product class with DTML/ZPT
methods, how can I make those methods members of the class rather than
simply instance methods? These examples should illustrate what I mean. 


My first try was like this: 
class HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item): 
        def __init__(self,resourcePath, imageFolder): 
               self.my_method = Globals.HTMLFile("dtml/my_method",globals())



That works fine until you want to add a new method to your class like so: class HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item): 
        def __init__(self,resourcePath, imageFolder): 
               self.my_method = Globals.HTMLFile("dtml/my_method",globals())

               self.my_new_method =
Globals.HTMLFile("dtml/my_new_method",globals()) 


new instances of your product will get the my_new_method, but instances that
have already been created will not. 


My second try was like this: 
class HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item): 
        def my_method(self, REQUEST): 
                """ template """ 
                if (not hasattr(self,"my_method_member")): 
                        self.my_method_member =
Globals.DTMLFile("dtml/my_method",globals()) 
                return self.my_method_member(self,REQUEST) 


This allows me to add methods to the class at any time and all instances
will get the method. However, for some reason, when I use this pattern, i
get strange acquisition results (for example, some of my objects children's
dtml methods will use the parent's namespace for lookups). 


Currently, I'm doing this: 
class HyperTextArea(Implicit, Globals.Persistent, RoleManager, Item): 
        def my_method(self, REQUEST): 
                """ template """ 
                self.my_method_member =
Globals.DTMLFile("dtml/my_method",globals()) 
                return self.my_method_member(self,REQUEST) 
which seems ugly, and like it might have performance problems, but i haven't
had any acquisition problems yet. 


My real question here is: What is the standard way to handle this problem of
being able to add dtml/zpt methods to classes such that all instances of
those classes get the new methods? 


Thanks in advance for any insight. 


Sincerely, 
Keith Alperin 


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************



More information about the Zope mailing list