velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rene Parra <>
Subject Re: Velocimacro argument (by-name) passing
Date Fri, 16 Jul 2004 17:32:26 GMT

Nathan Bubna <> wrote:

Christoph Reck said:
> Rene Parra wrote:
> > The following fails (because you can't double-quote a piece of
> > VM code that already contains a double-quote)
> >
> > #reusableStruct( "#center( "#bold('hello - 1st div')" )" "#center(
"#bold('hello - 2nd div')" )" )
> ^ ^ ^
> Just leave away the quotes. Macros always return strings. The following
> is untested, but to my knowledge should work:
> #reusableStruct( #center( #bold('hello - 1st div') ) #center(
#bold('hello - 2nd div') ) )

actually, i'm pretty sure i have tried this before and gotten template parse
errors. though, i don't recall which version of velocity i was using at the
time. so i've always used the double quotes since. and for complex things as
described, i've just used preceding #set() statements to simplify them:

Yes I ran into this problem as well.

#set( $bolddiv1 = "#bold( 'hello - 1st div' )" )
#set( $bolddiv2 = "#bold( 'hello - 2nd div' )" )
#reusableStruct( "#center( $bolddiv1 )" "#center( $bolddiv2 )" )

not only will this work, but i think it's a lot more readable and

The only problem I see with this approach however, is that $bolddiv1 will evaluate the RHS
expression before I invoke #reusableStruct.  If I want to defer the evaluation to the point
that it is being called within #reusableStruct(...) (i.e. passing the argument by name), 
I lose.

The biggest example of this is what if I pass in an argument to #bold() that only 'makes sense'
within the body of #center?

Assume #center() is defined as follows:

#macro(center $content)
  #set ($innerVar="Center Label")

The following may result in an error since $innerVar may not yet be in context.

#set ($bolddiv1 = "#bold( '$innerVar hello - 1st div')")

The only solution that I have found is to create a macro for each "deferred content".
For example:

#macro (bolddiv1)
  #bold( "$innerVar hello - 1st div")

#macro (bolddiv2)
  #bold( "$innerVar hello - 2st div")

#reusableStruct( "#bolddiv1()" "#bolddiv2()")

This gives me the most readability without sacrificing flexibility in terms of argument-evaluation

Any drawbacks to this approach?

Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message