[Zope] using ZPT to invoke an External Method and pass in variables

Tim Lynch lynch@gould.mannlib.cornell.edu
Thu, 03 Oct 2002 23:07:21 EDT


I've been trying to get a page template to call an external method, passing some
variables in the call.   I've been trying to follow an example I found on
ZopeLabs:
(http://www.zopelabs.com/cookbook/1008969252):


    Submitted by: runyaga
    Last Edited: 2002-02-18 
    Category: Python(External Method)
    
    ---------------------------------------------------------------------
    Description:
    if you want to use any python libraries, or xmlrpclib you can do this 
    from python external methods. this is very simple.
    ---------------------------------------------------------------------
    
    Source (Text): 
    #inside $ZOPE/Extensions
    #create a file, xmlrpc_example.py
    from xmlrpclib import Server
    
    
    def getStateNameByNumber(number=1):
        """ python lib example """
        # simple test program (from the XML-RPC specification)
        # server = Server("http://localhost:8000") # local server
        server = Server("http://betty.userland.com")
        try:
            return server.examples.getStateName(number)
        except Error, v:
            raise v
    
    #inside ZOPE create a External Method:
    #id: getStateName
    #module: xmlrpc_example
    #function: getStateNameByNumber
    
    #then you can test that or call it from a Pagetemplate:
    <span tal:define="stateNumber python:33">
      The #<span tal:replace="stateNumber"/> is 
           <span tal:replace="python:getStateName(stateNumber)" />
    </span>
 

When I follow this as given, I get:

Error Type: NameError
Error Value: global name 'getStateName' is not defined



If I revise the ext. method call to:
            <span tal:replace="here/getStateName" />

the call succeeds (using the default value) but I can't figure
out how to pass in a value.  Anything I try that has a variable
in parentheses following the function name fails.

The external method is OK as I've tested it outside of zope.
(actually, the ext. method is not actually making an xml-rpc call
at all -- I'm just returning whatever number is passed in as a sring)

I've pored over the mail list archives, checked zopelabs for other
examples, to no avail.


I'm running Zope 2.60b1 if that makes any difference.


- Tim Lynch
  tlynch@nal.usda.gov