velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <ge...@optonline.net>
Subject Re: velocimacro as parameter
Date Fri, 05 Oct 2001 12:32:26 GMT
On 10/5/01 8:26 AM, "Guillaume Mathe" <gmathe@smartinnov.com> wrote:

> Hi Geir,
> 
> Could you copy/paste all this in a FAQ and put it on the site ?
> I've fallen on this case before and i had to figure out by myself. This is
> exactly the kind of very useful info i was after...

Sure - which part? The whole thing?

> 
> Regards,
> Guillaume Mathe
> 
> Original message from: "Geir Magnusson Jr."
>> On 10/5/01 6:18 AM, "Andrea Papotti" <bill@ettelematica.it> 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
>> #end
>> 
>> #macro( outer $foo )
>>   #set($bar = "outerlala")
>>   outer : $foo
>> #end
>> 
>> #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>
>> #end
>> 
>> #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. :)
>> 
>> geir
>> 
>> 
>>> or something similar?
>>> 
>>> I.E. Is it possible to use a valocimacro as parameter of another one?
>>> 
>> 
>> -- 
>> Geir Magnusson Jr.     geirm@optonline.net
>> System and Software Consulting
>> "They that can give up essential liberty to obtain a little temporary safety
>> deserve neither liberty nor safety." - Benjamin Franklin
>> 
>> 
>> 
> 

-- 
Geir Magnusson Jr.     geirm@optonline.net
System and Software Consulting
"Whoever would overthrow the liberty of a nation must begin by subduing the
freeness of speech." - Benjamin Franklin



Mime
View raw message