[Zope] adding dtml/zpt methods to product classes

Keith Alperin kalperin at mac.com
Thu Jan 27 22:59:17 EST 2005


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

  
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 3300 bytes
Desc: not available
Url : http://mail.zope.org/pipermail/zope/attachments/20050127/ce39d294/attachment.bin


More information about the Zope mailing list