velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <>
Subject Re: velocimacro as parameter
Date Fri, 05 Oct 2001 10:33:05 GMT
On 10/5/01 6:18 AM, "Andrea Papotti" <> wrote:

> can I define something like:
> #macro (bold $text)
> <b>$text</b>
> #end
> #macro (center $what)
> <p align="center"> $what </p>
> #end
> and then use
> #center( #bold( "Hello world") )

No.  A directive isn't a valid argument to a directive.
> or
> #center( "#bold( "Hello world")" )

Yes, that will work fine, but watch those nested ", which won't work.  Do

#center( "#bold( 'Hello world' )" )

And you can always do something like

#set($foo = "#bold('Hello World')")
#center( $foo )

to be clear.  I say that (and stress that) as in the case of

#center( "#bold('hello world')" )

The arg is evaluated *inside* the VM, not at the 'calling level', so you can
get away with very wacky things like

#macro( inner $foo )
  inner : $foo

#macro( outer $foo )
   #set($bar = "outerlala")
   outer : $foo

#set($bar = 'calltimelala')
#outer( "#inner($bar)" )

Where the output is

Outer : inner : outerlala

Because the evaluation of the "#inner($bar)" happens inside #outer(), so the
$bar value set inside #outer() is the one that's used.

This is an intentional and jealously guarded feature - args are passed 'by
name' into VMs, so you can hand VMs things like stateful references such as

#macro( foo $color )
  <tr bgcolor=$color><td>Hi</td></tr>
  <tr bgcolor=$color><td>There</td></tr>

#foo( $bar.rowColor() )

And have rowColor() called repeatedly, rather than just once....

To avoid that

#set($color = $bar.rowColor())
#foo( $color )

Hope this wasn't too much info. :)


> or something similar?
> I.E. Is it possible to use a valocimacro as parameter of another one?

Geir Magnusson Jr.
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin

View raw message