You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Sriram Gopalan <sg...@salesforce.com> on 2006/05/11 20:10:35 UTC

Nested, cartesian loop of dynamic values

Hi, Here is my requirement. I have a list of function objects in java
that I can put into my velocity context. All these functions (methods)
take one parameter each. I also have this list of parameters that the
functions take. I need to be able to print the invoked return value of
these methods into my output file.
 
As an example, assume my functions are:
 
functions = {'getName', 'getAge','getSalary'...};
parameters = {'John','Joe','Jack','Mary'...}
 
and I want the output to be of the following format:
 
getName('John') = John Smith
getName('Joe') = Joe Doe
.
.
.
getAge('Jack') = 36
.
.
.
getSalary('Mary') = 30000
.
.
.
 
and so on. I hope my predicament is clear. The left hand side of the
output lines are easy ($function.name, $parameter.name etc.) However,
the right hand side is dynamic and actually depends on the runtime
evaluated output value of invoking $function on $parameter. What are the
various ways in which I can do this? What would my template look like?
 
#foreach ($function in $functions)
    #foreach ($parameter in $parameters)
 
$function.name('$parameter.name') = ## ???? WHAT COMES HERE?
 
    #end
#end
 
Thanks for the help in advance.
 
regards,
Sriram Gopalan

Re: Nested, cartesian loop of dynamic values

Posted by Claude Brisson <cl...@renegat.net>.
Hi

You should check the RenderTool (or the ViewRenderTool if you are using
the VelocityViewServlet).

http://jakarta.apache.org/velocity/tools/generic/RenderTool.html

To use the RenderTool tool, you've got to put it in the context, as long
as the context itself. You can then write things like

${function}('$parameter') =
$render.eval($context,"${function}.invoke($parameter)")

(I'm kinda gessing the 'invoke' syntax since I donno your function
objects...)

Considering your problem from another perspective, you could also maybe
use reflection... but you should give more details about your function
objects for hints about this option.

  Claude

Le jeudi 11 mai 2006 à 11:10 -0700, Sriram Gopalan a écrit :
> Hi, Here is my requirement. I have a list of function objects in java
> that I can put into my velocity context. All these functions (methods)
> take one parameter each. I also have this list of parameters that the
> functions take. I need to be able to print the invoked return value of
> these methods into my output file.
>  
> As an example, assume my functions are:
>  
> functions = {'getName', 'getAge','getSalary'...};
> parameters = {'John','Joe','Jack','Mary'...}
>  
> and I want the output to be of the following format:
>  
> getName('John') = John Smith
> getName('Joe') = Joe Doe
> .
> .
> .
> getAge('Jack') = 36
> .
> .
> .
> getSalary('Mary') = 30000
> .
> .
> .
>  
> and so on. I hope my predicament is clear. The left hand side of the
> output lines are easy ($function.name, $parameter.name etc.) However,
> the right hand side is dynamic and actually depends on the runtime
> evaluated output value of invoking $function on $parameter. What are the
> various ways in which I can do this? What would my template look like?
>  
> #foreach ($function in $functions)
>     #foreach ($parameter in $parameters)
>  
> $function.name('$parameter.name') = ## ???? WHAT COMES HERE?
>  
>     #end
> #end
>  
> Thanks for the help in advance.
>  
> regards,
> Sriram Gopalan


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


Re: Nested, cartesian loop of dynamic values

Posted by ap...@recks.org.
Claude pointed already in the right direction.
Your inquiry is clear enough to give a concrete
(untested) code snippet using the RenderTool:

#foreach ($function in $functions)
   #foreach ($parameter in $parameters)
     $render.eval($ctx, "${object}.${function}(${parameter})")
   #end
#end

Your example was missing the object onto which the
function is to be applied.

The example above has the context object name in
the $object context variable.

If the $object reference name is known at template
writing time, you can use the the reference name
directly, but it must be escaped until the eval call:
     $render.eval($ctx, "\$object.${function}(${parameter})")

Cheers,
Christoph

Sriram Gopalan wrote:
> Hi, Here is my requirement. I have a list of function objects in java
> that I can put into my velocity context. All these functions (methods)
> take one parameter each. I also have this list of parameters that the
> functions take. I need to be able to print the invoked return value of
> these methods into my output file.
>  
> As an example, assume my functions are:
>  
> functions = {'getName', 'getAge','getSalary'...};
> parameters = {'John','Joe','Jack','Mary'...}
>  
> and I want the output to be of the following format:
>  
> getName('John') = John Smith
> getName('Joe') = Joe Doe
> .
> .
> .
> getAge('Jack') = 36
> .
> .
> .
> getSalary('Mary') = 30000
> .
> .
> .
>  
> and so on. I hope my predicament is clear. The left hand side of the
> output lines are easy ($function.name, $parameter.name etc.) However,
> the right hand side is dynamic and actually depends on the runtime
> evaluated output value of invoking $function on $parameter. What are the
> various ways in which I can do this? What would my template look like?
>  
> #foreach ($function in $functions)
>     #foreach ($parameter in $parameters)
>  
> $function.name('$parameter.name') = ## ???? WHAT COMES HERE?
>  
>     #end
> #end
>  
> Thanks for the help in advance.
>  
> regards,
> Sriram Gopalan
> 

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