[Zope] dtml-var syntax - am i being dumb ?

Jeff K. Hoffman jeff.hoffman@goingv.com
Wed, 23 Feb 2000 13:48:46 -0500 (EST)


On Wed, 23 Feb 2000, Richard Moon wrote:

> Please stop me going mad.
> 
> I created two python method. The first , called 'know_nothing' takes no 
> parameters but returns the string "I know nothing",
> the second, called 'timesit" takes two parameters, multiplies them and 
> returns the result. When I come to use them from within
> a DTML document why do I have to say
> <dtml-var "timesit(1,2)">
> but
> <dtml-var know_nothing>
> 
> I assume the top one is short for
> <dtml-var expr="timesit(1,2)">

This is correct.

> whereas the second is short for
> <dtml-var name=know_nothing> (or even <dtml-var name="know_nothing"> since 
> that seems to work as well)
>
> (am I right ?)

Yes you are, on both counts.

> and I think I understand that the name attribute is actually a function 
> call with no parameters - right  ?

Yes. The ZPublisher adds the required parameters when it invokes the
method.

> but why does
> <dtml-var expr="know_nothing" not return anything to the calling object ?

Because expr="know_nothing" is not actually _calling_ the know_nothing()
method, it is simply referencing it. In Python, doing this would return
a function object:

  <function know_nothing at d9a20>

This may be what was returned by your expression dtml-var above (you can
tell by looking at view source; your browser will try and render that as a
<function> tag, and ignore it, so you won't see it on the page.)

You can use <dtml-var expr="know_nothing()">, and ZPublisher will fill in
the required parameters for you. Or, if you like, you can specify them
yourself. The prototype for DTMLMethod.__call__ is:

  def __call__(self, client=None, REQUEST={}, RESPONSE=None, **kw)

where:

  - client is commonly _.None, or this()
  - REQUEST is commonly _
  - and you can specify variable values with keyword arguments

If you want to call timest with two named parameters, val1 and val2, try:

  <dtml-var expr="timest(_.None, _, val1=1, val2=2)">

There is a lot more to this than what I've discussed here. Unfortunately,
I know of no good reference or tutorial on the subject.

> Just when you think you are getting the hang of it :-)

It has a tendency to sneak up on you like that. Don't let it freak you
out, though; it's all worth it.

> Richard Moon

--Jeff

---
Jeff K. Hoffman                                         704.849.0731 x108
Chief Technology Officer                                mailto:jeff@goingv.com
Going Virtual, L.L.C.                                   http://www.goingv.com/