[ZPT] ZPT in Python products, and _setName

Steve Alexander steve@cat-box.net
Sat, 13 Oct 2001 19:44:39 +0100


::: Making _setName work :::

About the _setName method:

You can use this method on DTML methods when you alias a name
in a product.

If you have something like this in your Python product:

     security.declarePublic('testPage')
     testPage = PageTemplateFile(_dtml_path+'testPage', globals())

     index_html=testPage

This will raise the warning:

2001-10-13T18:27:02 PROBLEM(100) Init Ambiguous name for method of
Products.MetalTest.MetalTest.MetalTest: "index_html" != "testPage"

If testpage were a DTMLFile, you'd do this:

     security.declarePublic('testPage')
     testPage = PageTemplateFile(_dtml_path+'testPage', globals())
     testPage._setName('testPage')

     index_html=testPage

There are many examples of this in the Zope core.


However, ZPT doesn't support this.

_setName is defined in ClassicHTMLFile of App/special_dtml.py.

     def _setName(self, name):
         self.__name__ = name
         self._need__name__ = 0

In order to get it working with PageTemplateFile, this should ideally 
just go in a base class common to DTMLFile, PageTemplateFile and XXXFile 
classes.

However, there isn't such a base class.


As a pragmatic solution, I've just added the method above to 
PageTemplateFile. It works for me.

Here's a patch against current CVS:


*** PageTemplateFile.py.original
--- ./PageTemplateFile.py.new
***************
*** 130,135 ****
--- 130,139 ----
           self.__name__ = name
           self.filename = filename = os.path.join(_prefix, filename + 
'.zpt')

+     def _setName(self, name):
+         self.__name__ = name
+         self._need__name__ = 0
+
       def pt_getContext(self):
           root = self.getPhysicalRoot()
           c = {'template': self,



--
Steve Alexander
Software Engineer
Cat-Box limited