[ZPT] passing values to external methods from a template
Tim Lynch
lynch@gould.mannlib.cornell.edu
Thu, 03 Oct 2002 17:16:55 EDT
I've been trying to get a page template call to an external method
working. I've been working from 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.
The external method is OK; I've tested it outside of zope.
(actually, my version is not making an xml-rpc call at all --
it's just returning whatever number is passed in as a sring)
I'm running Zope 2.60b1
So, how do I invoke an external method and pass it a value?
----
- Tim Lynch
tlynch@nal.usda.gov