You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "Ravikanth.L" <ra...@yahoo.com> on 2004/08/25 12:01:06 UTC

constructing variable names dynamically

Hi

 I am new to this velocity framework. I have a
scenario where in i should be able to dynamically
create a variable name used in template.

Eg:

 I have a arraylist of elements in $elements

 #foreach( $item in $elements )
   //need to create a variable with the value given by
getElementName() and assign the value
   #set($elementName = $item.getElementName())
   #set($$elementName = $item.getValue())

i.e if my elmementName is "Name" i would like to
create a variable name as $Name and assign the value
to this variable and use in the template.

Any suggestions are welcome

With Regards,
Ravikanth.L



		
_______________________________
Do you Yahoo!?
Win 1 of 4,000 free domain names from Yahoo! Enter now.
http://promotions.yahoo.com/goldrush

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


Re: constructing variable names dynamically

Posted by Shinobu Kawai <sh...@ieee.org>.
Hi Ravikanth,

>  Thanks for your immediate reply. I was going through
> the mail archive and render tool as well. But the
> problem here is, I am running a standalone
> application. But the VelocityTools require to modify
> the web.xml to include VelocityViewServlet and toolbox
> properties. In my case i won't be having that. Can you
> please give me some sample code which can be used to
> solve my problem for standalone application.
Actually, RenderTool is under the o.a.v.t.generic package, which means
that you can use them anywhere you want.  (You must be looking at
ViewRenderTool.)

So you can just:
    VelocityContext context = new VelocityContext();
    context.put("ctx", context);
    context.put("render", new RenderTool());
and you're off!

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <shinobu@ieee.org, shinobu@computer.org>



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


Re: constructing variable names dynamically

Posted by "Ravikanth.L" <ra...@yahoo.com>.
Hi All,

 I am facing some problem in using renderTool recurse
when used in macro which will be recursively executed.

The scenario is:
I am having an object which contains elements and its
associated values and the list child elements which
should be recursively processed same as the parent
element.

I was using the following:
#macro( parseItem $catItem )
#foreach( $element in $catItem.getElementList())##
 ##loop through the element list for item and add all 
the elements to context
 #set($name = $!element.getName())##
 #foreach( $value in $element.getValueList())
   #set($theValue = false)
   #set($theValue = $!value.getValue())##
   $render.recurse($ctx,'\#set($$name = $theValue)') 
   #if($$name == "id") <!-- id value : $id and   
theValue is : $theValue --> #end
 #end ##end of value list 
#end ##end of element list
#end ##end of macro

When I execute the template recursively, elements in
the top most object are printing correctly. But when i
take the child element for that object and i called
the same macro recursively from within the macro, the
variable names which are dynamically generated using
recurse statement are not having the new value (i.e
the child element value), it still holds the parent
element value though the child element value is not
null.

For example :

I have the hierarchy

--- 001 (id)
    --002
       -- 003

$id for the top level is printing properly, but when i
try to recursively call the same macro , for child
object , $id is still printing 001 instead of 002. But
the $theValue used in the template is giving 002 but
where as $id which has been generated dynamically is
still showing 001.

Can anyone please help me in solving this issue. I
tried clearing the contents of elements in the
beginning like 
$render.recurse($ctx,'\#set($$name = "")') 
before resetting to the new value. Still no success. 

Please correct me if i am doing something wrong.

With Regards,
Ravikanth.L



		
_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.yahoo.com

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


Re: constructing variable names dynamically

Posted by Shinobu Kawai <sh...@ieee.org>.
Hi Ravikanth,

Sorry for all those duplicate postings.  Gotta be patient. :)

> i tried with the following code
> 
>  \\ in my java code 
>   context.put("element","name");
>   context.put("elementValue","Ravi");
> 
>  i want to create a variable with name $name and its
> value should be "Ravi"
> 
> \\in my template i tried like this:
>  
>  render.recurse("#set($$element = $elementValue)")
>  Hello $name // as my template already using $name to
> display name.
Ah, /now/ I understand what you are trying to do!

First, you must understand what RenderTool#recurse() does.
    http://jakarta.apache.org/velocity/tools/javadoc/org/apache/velocity/tools/generic/RenderTool.html#recurse(org.apache.velocity.context.Context,%20java.lang.String)

#eval is easy to understand.  It just processes the given String once,
and that's it.  Now, with #recurse, think of it as the result of
processing #eval again and again, until nothing changes anymore.

In your case, the #set directive will be processed on the first round,
leaving nothing for the second.  What you have to do with #recurse, is
to think backwards:

You want to set "Ravi" to $name.  How do you that normally?
    #set($name = "Ravi")
You want to produce the text "#set($name = "Ravi")".  How is it done?
    \#set($$element = "$elementValue")
Note that the "#" before "set" is being escaped, so it won't be
processed here.

So what you need is:
    $render.recurse($ctx, '\#set($$element = "$elementValue")')

Very confusing at first, I must admit.  ;)

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <shinobu@ieee.org, shinobu@computer.org>



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


Re: constructing variable names dynamically

Posted by "Ravikanth.L" <ra...@yahoo.com>.
Hi Shinobu Kawai,

 Thanks for your code. I think I have some syntax
problem.

i tried with the following code

 \\ in my java code 
  context.put("element","name");
  context.put("elementValue","Ravi");

 i want to create a variable with name $name and its
value should be "Ravi"

\\in my template i tried like this:
 
 render.recurse("#set($$element = $elementValue)")
 Hello $name // as my template already using $name to
display name.

I tried with eval function also. No luck. 

Could you please correct me.

With Regards,
Ravikanth.L


	
		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail 

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


Re: constructing variable names dynamically

Posted by Shinobu Kawai <sh...@ieee.org>.
Hi Ravikanth,

>  Thanks for your immediate reply. I was going through
> the mail archive and render tool as well. But the
> problem here is, I am running a standalone
> application. But the VelocityTools require to modify
> the web.xml to include VelocityViewServlet and toolbox
> properties. In my case i won't be having that. Can you
> please give me some sample code which can be used to
> solve my problem for standalone application.
Actually, RenderTool is under the o.a.v.t.generic package, which means
that you can use them anywhere you want.  (You must be looking at
ViewRenderTool.)

So you can just:
    VelocityContext context = new VelocityContext();
    context.put("ctx", context);
    context.put("render", new RenderTool());
and you're off!

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <shinobu@ieee.org, shinobu@computer.org>



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


Re: constructing variable names dynamically

Posted by Shinobu Kawai <sh...@ieee.org>.
Hi Ravikanth,

>  Thanks for your immediate reply. I was going through
> the mail archive and render tool as well. But the
> problem here is, I am running a standalone
> application. But the VelocityTools require to modify
> the web.xml to include VelocityViewServlet and toolbox
> properties. In my case i won't be having that. Can you
> please give me some sample code which can be used to
> solve my problem for standalone application.
Actually, RenderTool is under the o.a.v.t.generic package, which means
that you can use them anywhere you want.  (You must be looking at
ViewRenderTool.)

So you can just:
    VelocityContext context = new VelocityContext();
    context.put("ctx", context);
    context.put("render", new RenderTool());
and you're off!

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <shinobu@ieee.org, shinobu@computer.org>



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


Re: constructing variable names dynamically

Posted by "Ravikanth.L" <ra...@yahoo.com>.
Hi
Thanks Mike Kienenberger and Shinobu Kawai for your
valuable suggestions. I have used the method suggested
by Shinobu Kawai. Its working perfectly fine. 

 Thankyou very much...

With Regards,
Ravikanth.L



		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - Send 10MB messages!
http://promotions.yahoo.com/new_mail 

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


Re: constructing variable names dynamically

Posted by Mike Kienenberger <mk...@alaska.net>.
"Ravikanth.L" <ra...@yahoo.com> wrote:
> it in the context. But still i am not getting the
> exact syntax to use dynamic variables.
> 
>  i tried with the following code
> 
>  \\ in my java code 
>   context.put("element","name");
>   context.put("elementValue","Ravi");
> 
>  i want to create a variable with name $name and its
> value should be "Ravi"
> 
> \\in my template i tried like this:
>  
>  render.recurse("#set($$element = $elementValue)")
>  Hello $name // as my template already using $name to
> display name.

You probably also need to do something like this:

	context.put("dollarSymbol","$");

I'm not sure what the exact syntax for the renderer is as I've never used 
it, but maybe...	render.recurse("#set(${dollarSymbol}${element} = 
${elementValue})")

or

	render.recurse("#set(${dollarSymbol}${dollarSymbol}element = 
${dollarSymbol}elementValue)")

However, why not just set all of these values from your java code?

-Mike

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


Re: constructing variable names dynamically

Posted by "Ravikanth.L" <ra...@yahoo.com>.
Hi,

 Now, I am able to access the ViewRenderTool from
standalone application also by keeping the instance of
it in the context. But still i am not getting the
exact syntax to use dynamic variables.

 i tried with the following code

 \\ in my java code 
  context.put("element","name");
  context.put("elementValue","Ravi");

 i want to create a variable with name $name and its
value should be "Ravi"

\\in my template i tried like this:
 
 render.recurse("#set($$element = $elementValue)")
 Hello $name // as my template already using $name to
display name.

I tried with eval function also. No luck. 

I know, there will be some problem in my syntax.  I am
very new to this velocity framework. Some one please
correct me.
 
With Regards,
Ravikanth.L 



	
		
__________________________________
Do you Yahoo!?
New and Improved Yahoo! Mail - 100MB free storage!
http://promotions.yahoo.com/new_mail 

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


Re: constructing variable names dynamically

Posted by "Ravikanth.L" <ra...@yahoo.com>.
Hi Shinobu Kawai,

 Thanks for your immediate reply. I was going through
the mail archive and render tool as well. But the
problem here is, I am running a standalone
application. But the VelocityTools require to modify
the web.xml to include VelocityViewServlet and toolbox
properties. In my case i won't be having that. Can you
please give me some sample code which can be used to
solve my problem for standalone application.

With Regards,
Ravikanth.L




		
__________________________________
Do you Yahoo!?
Yahoo! Mail - 50x more storage than other providers!
http://promotions.yahoo.com/new_mail

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


Re: constructing variable names dynamically

Posted by Shinobu Kawai <sh...@ieee.org>.
Hi Ravikanth,

>  I am new to this velocity framework. I have a
> scenario where in i should be able to dynamically
> create a variable name used in template.
I love this question.  I think I answered it no more than 24 hours ago. 
Anyways, I'll give you the same answer.  ;)

RenderTool is what you are seeking:
    http://jakarta.apache.org/velocity/tools/javadoc/org/apache/velocity/tools/generic/RenderTool.html

Since you are new here, I will lead you to a wonderful search site:
    http://www.mail-archive.com/velocity-user@jakarta.apache.org/

Best regards,
-- Shinobu Kawai

--
Shinobu Kawai <shinobu@ieee.org, shinobu@computer.org>




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