You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Guillaume Barré <Gu...@technomedia.ca> on 2004/03/08 16:13:16 UTC

help on macro

Hi all,

 

I'm new to Velocity and I don't know if is it possible to do something like the following sample in a macro :

 

Sample :

 

#macro (displaylangs $fieldname $scdata $langs)

            #set ($name = "${fieldname}_")

            #foreach ($langue in $langs)

                        <tr>

                        #set ($field = "$name$langue")

                        #if ($!scdata.BEAN.Name)

                                    #formCell ($l10n.get($langue) $field $!scdata.BEAN.Name.get($langue))

                #else                        

                                    #formCell ($l10n.get($langue) $field "")

                        #end     

                        </tr>

            #end

#end

 

My problem is that I want to change the "BEAN" parameter return by the macro. For example for a first call I want the macro to write "$!scdata.BEAN.Name.get($langue))" and for a second call I want "$!scdata.BEAN.Description.get($langue))"

 

I tried to do that by adding a new parameter "#macro (displaylangs $fieldname $beanfieldname $scdata $langs)" and looked for something like :

 

#formCell ($l10n.get($langue) $field $!scdata.BEAN. $beanfieldname.get($langue))

 

So is it possible to do this type of process with Velocity ???

 

Regards, and thanks in advance.

Guillaume

 

 

 


Re: help on macro

Posted by Christoph Reck <ap...@recks.org>.
Hi,

Guillaume Barré wrote:
> Hi all,
> 
> I'm new to Velocity and I don't know if is it possible to do something 
> like the following sample in a macro :
> 
> Sample :
> 
> #macro (displaylangs $fieldname $scdata $langs)
>             #set ($name = "${fieldname}_")
>             #foreach ($langue in $langs)
>                         <tr>
>                         #set ($field = "$name$langue")
>                         #if ($!scdata.BEAN.Name)

To my knowledge, the bang character ! will not work here, when
macros are evaluated, the references in the body must match
the declaration (at least this used to be so in earlyer days).
Anyway, within an #if you don't need the silent notation (though,
it will emit a message into the log).

> 
>                                     #formCell ($l10n.get($langue) $field $!scdata.BEAN.Name.get($langue))
>                 #else                        
>                                     #formCell ($l10n.get($langue) $field "")
>                         #end     
>                         </tr>
>             #end
> #end
>  
> My problem is that I want to change the "BEAN" parameter 
> return by the macro. For example for a first call I want 
> the macro to write "$!scdata.BEAN.Name.get($langue))" and 
> for a second call I want "$!scdata.BEAN.Description.get($langue))"

Even though I don't understand wholly what you are trying to
explain above, I'm guessing that you want to dynamically evaluate
the bean attribute. For this you can use a tool from the velocity-tools

   #set( $D = '$' ) #* This is the is sure to work poor-mans escape *#

   $render.eval( "${D}scdata.BEAN.${D}beanfieldname.get($langue)" )

More on the render tool in the velocity-tools sub-project at:
   http://jakarta.apache.org/velocity/tools/generic/

> 
> I tried to do that by adding a new parameter 
> "#macro (displaylangs $fieldname $beanfieldname $scdata $langs)" 
> and looked for something like :
> 
> #formCell ($l10n.get($langue) $field $!scdata.BEAN. $beanfieldname.get($langue))
> 
> So is it possible to do this type of process with Velocity ???

Yes, see above.

> 
> Regards, and thanks in advance.
> 
> Guillaume
> 

-- 
:) Christoph Reck


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org