Hi,
if the macro returns a string, use the string-interpolation capability:
#set( $myVar = "#returnField($fieldPath $varName)" )
if the $varname (note the single '$', more on this below) returns an object, I
believe you can assign it either to a global variable (depends on the local
scope behavior set in the velocity properties - some changes happened here
towards release 1.6.2 - needs testing), or to one of the macro parameters (also
release 1.6.x/1.7.x changes something here in the pass by reference
functionality - needs testing):
#macro( call $foo )#if($foo)#**##end#end
#macro( returnField $fieldPath $varName )
...
#set( #myGlobal = $varName($fieldPath.field.id) )
#end
...
#set( $myGlobal = false )
#call( "#returnField($fieldPath $varName)" )
Your macro updated: $myGlobal
or
#macro( call $foo )#if($foo)#**##end#end
#macro( returnField $fieldPath $varName $returnVal )
...
#set( #returnVal = $varName($fieldPath.field.id) )
#end
...
#set( $returnVal = false )
#call( "#returnField( $fieldPath $varName $returnVal )" )
Your macro returned: $returnVal
Note 1: the call macro allows calling something but ignores any string output
generated within the macro (or a java method call).
Note 2: you cannot do indirect variable access (see $$varName in your example).
You could use the #evaluate directive or tool for this.
Note 3: this looks like too much programming in the template, it might be wise
to reconsider the architecture. Consider placing the macro functionality into a
java pojo tool and call it via the context - results in much cleaner template
code:
#set( $returnVal = $myTool.returnField( $fieldPath $varName )
Kind regards,
Christoph
bren36 wrote:
> Hi ,
>
> Is there a way that i can return a value from a velocity macro
>
> My macro looks something like so
>
> #macro(returnField $fieldPath $varName)
> #if($fieldPath.subfield)
> $$varName($fieldPath.field.id)
> #returnField ($varName $fieldPath.subfield )
> #end
> #end
>
> Where i need to return $$varName($fieldPath.field.id)
>
> how can i call the returnField macro and assign it to a variable ??????
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]