You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Scott Farquhar <sc...@atlassian.com> on 2003/02/11 15:10:56 UTC
Accessing context for ${${var.value}}
Gday all,
I've searched the archives for an answer to this, and the closest I have
come to is this:
http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg09093.html
However, it doesn't answer the question that I am about to ask.
How can you evaluate expressions of the type ${${var.value}}? You can
use context.get("var") but you _can't_ use context.get("var.value").
This is beacuse context.get() should only be used to get a value from
the context, and not parse an expression.
Why do I need to do this? Here is an example:
## "author" is a runtime variable normally
#set ($var1 = "author")
#foreach ($param in $parameters.list)
#set ($var2 = "$param.")
#set ($var3 = "$var1$var2") ## $var3 is "$param.author"
#context.get("$var3") ## this doesn't work.
#end
It is important to be able to get attributes of a variable, as well as
the variable itself at runtime. This is needed in a real world example,
as I am trying to convert the webwork UI tags to use velocity.
Is there a way to parse an expression at runtime? I tried looking
through the source - but I had no idea where to start.
If this is not possible, can you let me know, as I will have to evaluate
other templating frameworks.
Thanks for your time.
Scott
--
ATLASSIAN - http://www.atlassian.com
Expert J2EE Software, Services and Support
-------------------------------------------------------
Need a simple, powerful way to track and manage issues?
Try JIRA - http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Inner classes - Was: Accessing context for ${${var.value}}
Posted by Nathan Bubna <na...@esha.com>.
Scott said:
...
> INNER CLASSES
> -------------
>
> The next problem that I am having is regarding accessing inner class
> properties. Webwork's expression language can handle accessing inner
> classes, but it seems that velocity's can't (unless I am doing something
> wrong).
>
> As part of the webwork examples, you use the BookList class to iterate
> over books. A book however is an inner class. You can see the source
> for BookList.java here:
>
>
>
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/opensymphony/webwork/src/main
/webwork/examples/BookList.java?rev=1.4&content-type=text/vnd.viewcvs-markup
>
> Here's what happens:
> #foreach ($book in $booklist)
> $book ## prints "webwork.examples.BookList$1$Book@b09697"
> $book.title ## prints "$book.title"
> #end
>
> If I move Book into its own class, it works as expected.
do you declare it as public when you do that?
> Does velocity work with inner classes? Is there something that I need to
> be aware of?
hmm. well, i know the modifier 'public' is important to Velocity. although
I rarely use inner classes in templates, afaik, an object needs to be
declared as public for Velocity to be happy with it. i don't think it
matters whether it's an inner class or not.
if declaring it as public isn't enough, then i wonder if it's because it's
inside the static init block. perhaps try pulling it out of there.
Nathan Bubna
nathan@esha.com
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Inner classes - Was: Accessing context for ${${var.value}}
Posted by Scott Farquhar <sc...@atlassian.com>.
Thanks to Dennis & Bill who have helped me get this far! I am very
impressed by the responsiveness of this mailing list, and the quality of
the replies!
The final (working!) solution is:
## "author" is a runtime variable normally
#set ($var1 = "author")
#set ($var2 = '$param.')
#set ($var3 = "$var2$var1") ## $var3 is "${param.author}"
#foreach ($param in $parameters.list)
$webwork.evaluate("$var3")
#end
That works fine & does all that I need to do (although a bit ugly still).
INNER CLASSES
-------------
The next problem that I am having is regarding accessing inner class
properties. Webwork's expression language can handle accessing inner
classes, but it seems that velocity's can't (unless I am doing something
wrong).
As part of the webwork examples, you use the BookList class to iterate
over books. A book however is an inner class. You can see the source
for BookList.java here:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/opensymphony/webwork/src/main/webwork/examples/BookList.java?rev=1.4&content-type=text/vnd.viewcvs-markup
Here's what happens:
#foreach ($book in $booklist)
$book ## prints "webwork.examples.BookList$1$Book@b09697"
$book.title ## prints "$book.title"
#end
If I move Book into its own class, it works as expected.
Does velocity work with inner classes? Is there something that I need to
be aware of?
Cheers,
Scott
Bill Burton wrote:
> Hello Scott,
>
> Assuming you are invoking Velocity through a RequestDispatcher.include
> to the WebWorkVelocityServlet, it already instantiates WebWorkUtil only
> once per request. In this case, I suggest you modify WebWorkUtil and
> WebWorkVelocityServlet so after it's instantiated, you set the
> WebWorkVelocityContext object into it or passing it in via the
> constructor. Then your evaluate method would be simpler as:
> $webwork.eval('$var3')
>
> There would be no need to use #bean or pass the context into the method.
> If this is still too verbose for you, add a macro to the webwork.vm
> (same as VM_global_library.vm):
>
> #macro (eval text)
> $webwork.eval($text)#end## avoid a newline
>
> Now you can evaluate any string with: #eval('$var3') which is even more
> concise.
>
> Maybe you could find some other templating system that could do
> recursive evaluation but I doubt it would 10 x faster than the JSP
> solution you're replacing which is the whole reason for using Velocity
> in the first place.
>
> Note: Since most people on this list may not be familiar with the
> WebWork framework, here's some background on what Scott's doing
> http://sourceforge.net/mailarchive/message.php?msg_id=3722197. The main
> point being that Velocity is very fast.
>
> The strength of Velocity is that it strikes a good balance between
> simplicity and functionalality. It also just happens to be very fast as
> well. One of the reasons is the maintainers have avoided added feature
> after feature to make everyone happy.
>
> Hope this helps,
> -Bill
>
> Scott Farquhar wrote:
>
>>
>>
>> Denis wrote:
>>
>>> Use Velocity.evaluate() via a tool.
>>
>>
>>
>> Thanks for your help so far!
>>
>> I have written a simple wrapper:
>>
>> public String evaluate(Context ctx, String expression) throws Exception
>> {
>> CharArrayWriter writer = new CharArrayWriter();
>> Velocity.evaluate(ctx, writer,
>> "Error parsing " + expression, expression);
>> return writer.toString();
>> }
>>
>> However, the only way that I can get it to work is the following:
>>
>>
>> #bean ("webwork.view.velocity.WebworkUtil" $webwork)
>>
>> ## "author" is a runtime variable normally
>> #set ($var1 = "author")
>>
>> #foreach ($param in $parameters.list)
>> #set ($var2 = "${param.")
>> #set ($var3 = "$var2$var1}") ## $var3 is "${param.author}"
>>
>> $webwork.evaluate($context, "$var3")
>> #end
>>
>> Surely there is a better way for me to set var3 than the above, but I
>> can't find a way to escape it properly?
>>
>> What is the reason that ${${ }} doesn't work?
>>
>> Cheers,
>> Scott
>>
>>
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
--
ATLASSIAN - http://www.atlassian.com
Expert J2EE Software, Services and Support
-------------------------------------------------------
Need a simple, powerful way to track and manage issues?
Try JIRA - http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Accessing context for ${${var.value}}
Posted by Bill Burton <bi...@progress.com>.
Hello Scott,
Assuming you are invoking Velocity through a RequestDispatcher.include
to the WebWorkVelocityServlet, it already instantiates WebWorkUtil only
once per request. In this case, I suggest you modify WebWorkUtil and
WebWorkVelocityServlet so after it's instantiated, you set the
WebWorkVelocityContext object into it or passing it in via the
constructor. Then your evaluate method would be simpler as:
$webwork.eval('$var3')
There would be no need to use #bean or pass the context into the method.
If this is still too verbose for you, add a macro to the webwork.vm
(same as VM_global_library.vm):
#macro (eval text)
$webwork.eval($text)#end## avoid a newline
Now you can evaluate any string with: #eval('$var3') which is even more
concise.
Maybe you could find some other templating system that could do
recursive evaluation but I doubt it would 10 x faster than the JSP
solution you're replacing which is the whole reason for using Velocity
in the first place.
Note: Since most people on this list may not be familiar with the
WebWork framework, here's some background on what Scott's doing
http://sourceforge.net/mailarchive/message.php?msg_id=3722197. The main
point being that Velocity is very fast.
The strength of Velocity is that it strikes a good balance between
simplicity and functionalality. It also just happens to be very fast as
well. One of the reasons is the maintainers have avoided added feature
after feature to make everyone happy.
Hope this helps,
-Bill
Scott Farquhar wrote:
>
>
> Denis wrote:
>
>> Use Velocity.evaluate() via a tool.
>
>
> Thanks for your help so far!
>
> I have written a simple wrapper:
>
> public String evaluate(Context ctx, String expression) throws Exception
> {
> CharArrayWriter writer = new CharArrayWriter();
> Velocity.evaluate(ctx, writer,
> "Error parsing " + expression, expression);
> return writer.toString();
> }
>
> However, the only way that I can get it to work is the following:
>
>
> #bean ("webwork.view.velocity.WebworkUtil" $webwork)
>
> ## "author" is a runtime variable normally
> #set ($var1 = "author")
>
> #foreach ($param in $parameters.list)
> #set ($var2 = "${param.")
> #set ($var3 = "$var2$var1}") ## $var3 is "${param.author}"
>
> $webwork.evaluate($context, "$var3")
> #end
>
> Surely there is a better way for me to set var3 than the above, but I
> can't find a way to escape it properly?
>
> What is the reason that ${${ }} doesn't work?
>
> Cheers,
> Scott
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Accessing context for ${${var.value}}
Posted by Marvin Greenberg <mg...@dctd.saic.com>.
The other thing you could do is put a Map in your context, and do sets and
gets from it.
instead of ${${var}} do $globalMap.get(${var})
Which is essentially what velocity must have internally -- perhaps there is
a way to expose velocity's symbol table for wizardly applications?
----- Original Message -----
From: "Nathan Bubna" <na...@esha.com>
To: "Velocity Users List" <ve...@jakarta.apache.org>
Sent: Tuesday, February 11, 2003 10:48 AM
Subject: Re: Accessing context for ${${var.value}}
> Scott said:
> ...
> > I have written a simple wrapper:
> >
> > public String evaluate(Context ctx, String expression) throws Exception
> > {
> > CharArrayWriter writer = new CharArrayWriter();
> > Velocity.evaluate(ctx, writer,
> > "Error parsing " + expression, expression);
> > return writer.toString();
> > }
> >
> > However, the only way that I can get it to work is the following:
> >
> >
> > #bean ("webwork.view.velocity.WebworkUtil" $webwork)
> >
> > ## "author" is a runtime variable normally
> > #set ($var1 = "author")
> >
> > #foreach ($param in $parameters.list)
> > #set ($var2 = "${param.")
> > #set ($var3 = "$var2$var1}") ## $var3 is "${param.author}"
> >
> > $webwork.evaluate($context, "$var3")
> > #end
> >
> > Surely there is a better way for me to set var3 than the above, but I
> > can't find a way to escape it properly?
> >
> > What is the reason that ${${ }} doesn't work?
>
> It would require multiple evaluations. As Jon Stevens always said, "this
> isn't perl." Velocity is meant to be simple and quick.
>
> Though i haven't done it myself and thus don't know if it'd work, you
could
> try having your WebworkUtil tool continue evaluating the expression until
> either no more $ characters remain or the expression and its result match.
>
> Nathan Bubna
> nathan@esha.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Accessing context for ${${var.value}}
Posted by Nathan Bubna <na...@esha.com>.
Scott said:
...
> I have written a simple wrapper:
>
> public String evaluate(Context ctx, String expression) throws Exception
> {
> CharArrayWriter writer = new CharArrayWriter();
> Velocity.evaluate(ctx, writer,
> "Error parsing " + expression, expression);
> return writer.toString();
> }
>
> However, the only way that I can get it to work is the following:
>
>
> #bean ("webwork.view.velocity.WebworkUtil" $webwork)
>
> ## "author" is a runtime variable normally
> #set ($var1 = "author")
>
> #foreach ($param in $parameters.list)
> #set ($var2 = "${param.")
> #set ($var3 = "$var2$var1}") ## $var3 is "${param.author}"
>
> $webwork.evaluate($context, "$var3")
> #end
>
> Surely there is a better way for me to set var3 than the above, but I
> can't find a way to escape it properly?
>
> What is the reason that ${${ }} doesn't work?
It would require multiple evaluations. As Jon Stevens always said, "this
isn't perl." Velocity is meant to be simple and quick.
Though i haven't done it myself and thus don't know if it'd work, you could
try having your WebworkUtil tool continue evaluating the expression until
either no more $ characters remain or the expression and its result match.
Nathan Bubna
nathan@esha.com
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Accessing context for ${${var.value}}
Posted by Denis <br...@mac.com>.
> However, the only way that I can get it to work is the following:
>
>
> #bean ("webwork.view.velocity.WebworkUtil" $webwork)
>
> ## "author" is a runtime variable normally
> #set ($var1 = "author")
>
> #foreach ($param in $parameters.list)
> #set ($var2 = "${param.")
> #set ($var3 = "$var2$var1}") ## $var3 is "${param.author}"
>
> $webwork.evaluate($context, "$var3")
> #end
>
> Surely there is a better way for me to set var3 than the above, but I
> can't find a way to escape it properly?
Scott, don't forget that simple quotes (') and double quotes (") are
different in Velocity. If you write:
#set ($var2 = "$param.")
Then $param will be evaluated immediately in var2. If a parameter of
the list is "book", that will give you "book.author" in var3 (which is
not evaluated by velocity).
What you need is to use single quotes, as in:
#set ($var2 = '$param.')
This way $param is not evaluated and you should have "$param.author" in
var3.
Cheers,
-- Denis.
>
> What is the reason that ${${ }} doesn't work?
>
> Cheers,
> Scott
>
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Accessing context for ${${var.value}}
Posted by Scott Farquhar <sc...@atlassian.com>.
Denis wrote:
> Use Velocity.evaluate() via a tool.
Thanks for your help so far!
I have written a simple wrapper:
public String evaluate(Context ctx, String expression) throws Exception
{
CharArrayWriter writer = new CharArrayWriter();
Velocity.evaluate(ctx, writer,
"Error parsing " + expression, expression);
return writer.toString();
}
However, the only way that I can get it to work is the following:
#bean ("webwork.view.velocity.WebworkUtil" $webwork)
## "author" is a runtime variable normally
#set ($var1 = "author")
#foreach ($param in $parameters.list)
#set ($var2 = "${param.")
#set ($var3 = "$var2$var1}") ## $var3 is "${param.author}"
$webwork.evaluate($context, "$var3")
#end
Surely there is a better way for me to set var3 than the above, but I
can't find a way to escape it properly?
What is the reason that ${${ }} doesn't work?
Cheers,
Scott
--
ATLASSIAN - http://www.atlassian.com
Expert J2EE Software, Services and Support
-------------------------------------------------------
Need a simple, powerful way to track and manage issues?
Try JIRA - http://www.atlassian.com/software/jira
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org
Re: Accessing context for ${${var.value}}
Posted by Denis <br...@mac.com>.
Hi Scott,
On Tuesday, February 11, 2003, at 02:10 pm, Scott Farquhar wrote:
> Gday all,
>
> I've searched the archives for an answer to this, and the closest I
> have come to is this:
>
> http://www.mail-archive.com/velocity-user@jakarta.apache.org/
> msg09093.html
>
> However, it doesn't answer the question that I am about to ask.
>
> How can you evaluate expressions of the type ${${var.value}}? You can
> use context.get("var") but you _can't_ use context.get("var.value").
> This is beacuse context.get() should only be used to get a value from
> the context, and not parse an expression.
>
> Why do I need to do this? Here is an example:
>
> ## "author" is a runtime variable normally
> #set ($var1 = "author")
>
> #foreach ($param in $parameters.list)
> #set ($var2 = "$param.")
> #set ($var3 = "$var1$var2") ## $var3 is "$param.author"
This should be:
#set ($var3 = "$var2$var1") ## $var3 is "$param.author"
>
> #context.get("$var3") ## this doesn't work.
Use Velocity.evaluate() via a tool.
-- Denis.
> #end
>
>
> It is important to be able to get attributes of a variable, as well as
> the variable itself at runtime. This is needed in a real world
> example, as I am trying to convert the webwork UI tags to use > velocity.
>
> Is there a way to parse an expression at runtime? I tried looking
> through the source - but I had no idea where to start.
>
> If this is not possible, can you let me know, as I will have to
> evaluate other templating frameworks.
>
> Thanks for your time.
> Scott
> --
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org