You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by bren36 <tk...@gmail.com> on 2009/04/17 23:06:18 UTC
macro question
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 ??????
--
View this message in context: http://www.nabble.com/macro-question-tp23105606p23105606.html
Sent from the Velocity - Dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org
Re: macro question
Posted by ap...@recks.org.
Reconsidering the question, just use the velocity 1.6 #evaluate directive, see:
http://velocity.apache.org/engine/releases/velocity-1.6.2/user-guide.html#Evaluate
applied to your example (note the single quotes):
#evaluate('#set( $returnVal = $varName($fieldPath.field.id)')
Returned value: $returnVal
Let us know if this worked for you.
:) 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: dev-unsubscribe@velocity.apache.org
> For additional commands, e-mail: dev-help@velocity.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org
Re: macro question
Posted by ap...@recks.org.
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: dev-unsubscribe@velocity.apache.org
For additional commands, e-mail: dev-help@velocity.apache.org