How to write a method using DTML Method?
How would I convert the following Java code into DTML? This is just an example, I don't understand how methods are used in DTML. String getFirstName(String name) { String lastName = name.substring(0, name.indexOf(' ')); return (lastName); } I would like to see a DTML method which does this, how it is called and how the result is used in the calling method. Thanks. -- Guy Davis mailto:davis@arc.ab.ca (403) 210-5334 Alberta Research Council
Let's try it in Python. We can do it in DTML, but it just looks so much *nicer* in Python. def getFirstName(name): import string lastName=string.split(name, " ")[1] return lastName You can create an External Method that contains this code and it'll do what its Java counterpart does when you call it from DTML or from Python. To call it, we'd do something like this in DTML where listofUniqueFullNames is a SQL method that returns "SELECT DISTINCT FULL_NAME FROM NAMES", which returns a recordset object that contains an attribute FULL_NAMES which is a tuple of strings like ("John Smith", "Uncle Sam", ) <dtml-in listOfUniqueFullNames> <dtml-var "getFirstName(FULL_NAME)"> </dtml-in> This will return to a client browser: Smith Sam Guy Davis wrote:
How would I convert the following Java code into DTML? This is just an example, I don't understand how methods are used in DTML.
String getFirstName(String name) { String lastName = name.substring(0, name.indexOf(' ')); return (lastName); }
I would like to see a DTML method which does this, how it is called and how the result is used in the calling method. Thanks. -- Guy Davis mailto:davis@arc.ab.ca (403) 210-5334 Alberta Research Council
_______________________________________________ Zope maillist - Zope@zope.org http://lists.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://lists.zope.org/mailman/listinfo/zope-announce http://lists.zope.org/mailman/listinfo/zope-dev )
-- Chris McDonough - Digital Creations, Inc. Publishers of Zope - http://www.zope.org
How would I convert the following Java code into DTML? This is just an example, I don't understand how methods are used in DTML.
String getFirstName(String name) { String lastName = name.substring(0, name.indexOf(' ')); return (lastName); }
I would like to see a DTML method which does this, how it is called and how the result is used in the calling method. Thanks.
This sounds like more of a task for Python than DTML. Snippets of Python can be pretty easily embedded in DTML documents. Off the cuff (i.e. UNTESTED CODE FOLLOWS), here's how I'd do it: <dtml-var "name[:_.string.find(' ', name)]"> The Python above essentially mimics the behavior of your Java method. It returns a substring (or "slice", in Python) of "name" from the beginning of the string to the first occurrence of a space. If you know a thing or two about Python slices, this should probably make sense. If you don't, there's copious documentation at http://www.python.org (or just email me privately and I'll try to help). That half-line above is all you need, presuming "name" is defined somewhere in your namespace (e.g. as a document property or a variable passed from an HTML form). No DTML methods are necessary, as you can see, though you could almost as easily stick that line into a separate DTML method and insert it with: <dtml-var dtml-method-name> But I don't know why you'd want to do that. Instead, you probably want to include any part of the document which needs to use the first name in a dtml-let tag: <dtml-let first_name="name[:_.string.find(' ', name)]"> Hi there, <dtml-var first_name>! </dtml-let> Hope this helps. Regards, Ben
Guy Davis wrote:
How would I convert the following Java code into DTML? This is just an example, I don't understand how methods are used in DTML.
String getFirstName(String name) { String lastName = name.substring(0, name.indexOf(' ')); return (lastName); }
This could be written in one line of DTML as: <dtml-var expr="_.string.split(name)[0]"> The "_.string" part is a module of handy string methods. 'split' breaks a string at whitespace into a list of words, and '[0]' select the first of these. You don't declare parameters explicitly. Then, in a DTML Document or Method you might call this with: <p>Your first name is <dtml-var expr="getFirstName(name='Guy Davis')">.</p> or, if you were iterating through SQL records with a field 'name', you might write: <table> <dtml-in SQL_people> <tr><td>First Name: <dtml-var getFirstName></td><td>Age: <dtml-var age></td></tr> </dtml-in> </table> In this case, since we didn't pass 'name' explicitly, it is searched for in the namespace stack, and found in the current data record. Cheers, Evan @ 4-am
participants (4)
-
Ben Glazer -
Chris McDonough -
Evan Simpson -
Guy Davis