velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jonathan Revusky <...@revusky.com>
Subject Re: Indirect references
Date Sun, 10 Aug 2003 00:04:27 GMT
Claude Brisson wrote:

> This has been discussed not long ago, see http://www.mail-archive.com/velocity-user@jakarta.apache.org/msg10305.html
> 
> In a nutshell, "$$" is not implemented, some folks (including me) think it should, others
think it should not... and the most common
> workaround is to explicitely call generic getters from the VTL.
> 
> For your example, you need to place a reference to the context inside itself with :
> 
> [java] context.put("context",context)
> 
> and then do :
> 
> [VTL] #set($list = $context.get("$listname"))


Actually, that trick only works if $listname is directly the name of a 
variable. It wouldn't work in the more fully general case, where it's a 
more complex expression.

#set ($exp = "foo")

$context.get("$exp") ==> returns the same thing as $foo

OTOH, if the expression is compound, as in:

#set ($exp = "foo.bar.baz")

then that won't work.

Well, not to disappoint people... they figure I'm going to point out 
that this feature is available in FreeMarker. Actually, almost. Somebody 
requested something like this just a few days ago and it is implemented. 
It's currently in CVS and will be in the very next FM release. It works 
like this:

<#assign exp = "foo.bar.baz">

${exp?eval}

would output the exact same thing as:

${foo.bar.baz}

with the caveat that it's much less efficient, naturally, since it 
involves reparsing the expression each time it's evaluated. But 
sometimes people need this, I guess.

Regards,

Jonathan Revusky
-- 
lead developer, FreeMarker project, http://freemarker.org/
FreeMarker-Velocity comparison page, http://freemarker.org/fmVsVel.html



Mime
View raw message