[Zope-dev] More almost __call__ ;-)

Jim Fulton jim@digicool.com
Fri, 13 Oct 2000 09:03:41 -0400


Chris Withers wrote:
> 
> Toby Dickenson wrote:
> > <http://www.zope.org/Members/htrd/howto/FunctionTemplate>
> >
> > you would use....
> >
> >     def a_method(self,md):
> >         do_stuff_with(md['param1'],md['param2'])
> >     a_method = FunctionTemplate(a_method)
> 
> That looks like it'll do the trick... I wonder if there's any way you
> can role it up into a Product so that I don't need to have
> FunctionTemplate.py in each folder of a product that needs to use it?

Is it *really* the case that you often want to method to be called directly
via the Web *and* and from DTML?  This doesn't seem right to me.

In any case, if you *just* want to get your function to be
called from DTML (and your class is an extension class), you can give
it the isDocTemp attribute like so:

  class MyClass(.... include some extension class ...):

    some_methodisDocTemp=1
    def some_method(self, ignored, md):
        ....

Methods in extension classes can have (read-only) attributes
by defining an attribute whose name is the concatenation of the
method name and the attribute name.  Note that the method is
also callable from the web as usual. If you want it to be called
the same way as a template, you'd need to use:

  class MyClass(.... include some extension class ...):

    some_methodisDocTemp=1
    def some_method(trueself, self, REQUEST, RESPONSE=None):
        ....

In this example, 'trueself' is bound by Python and is the traditional
python self. The 'self' argument gets bound by the publisher
to the object the method was invoked on.  

Note that if you get called from the web, RESPONSE will
not be none. This is a handy way to decide if you
are called from the web or as a sub-template. Of course,
someone could pass a non-None RESPONSE argument directly.

Jim

--
Jim Fulton           mailto:jim@digicool.com   Python Powered!        
Technical Director   (888) 344-4332            http://www.python.org  
Digital Creations    http://www.digicool.com   http://www.zope.org    

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission.  Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.