[Zope] How to write a method using DTML Method?
Ben Glazer
glazer@scicomp.com
Tue, 11 Jan 2000 15:40:36 -0600
> 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