[Zope] How to write a method using DTML Method?

Evan Simpson evan@4-am.com
Tue, 11 Jan 2000 15:42:30 -0600


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