You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Ted Husted <hu...@apache.org> on 2003/08/08 20:11:00 UTC

Indirect references

This may sound odd, but, if it were possible, I'd like to do a bit of 
indirection in my variable references.

The use case is this: I have a list in the context. The list may end up 
under one name or another, but I do know what the list's name is before 
I forward to the template. I'd like the template to set a local variable 
to the list based on the name that I pass inside of another variable.

Essentially, something like this:

#set($list = ${$listname})

where I've already something like has already been done (in one place or 
another):

.set("someName",list);

... later ...

.set("listname","someName");

and have $list be a pointer to list in the first statement.

Possible?

-Ted.


-- 
Ted Husted,
   Junit in Action  - <http://www.manning.com/massol/>,
   Struts in Action - <http://husted.com/struts/book.html>,
   JSP Site Design  - <http://www.amazon.com/exec/obidos/ISBN=1861005512>.




Re: Indirect references

Posted by Jonathan Revusky <jo...@revusky.com>.
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



Re: Indirect references

Posted by Jonathan Revusky <jo...@revusky.com>.
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



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


Re: Indirect references

Posted by Claude Brisson <cl...@savoirweb.com>.
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"))

CloD

----- Original Message -----
From: "Ted Husted" <hu...@apache.org>
To: <ve...@jakarta.apache.org>
Sent: vendredi 8 août 2003 20:11
Subject: Indirect references


> This may sound odd, but, if it were possible, I'd like to do a bit of
> indirection in my variable references.
>
> The use case is this: I have a list in the context. The list may end up
> under one name or another, but I do know what the list's name is before
> I forward to the template. I'd like the template to set a local variable
> to the list based on the name that I pass inside of another variable.
>
> Essentially, something like this:
>
> #set($list = ${$listname})
>
> where I've already something like has already been done (in one place or
> another):
>
> .set("someName",list);
>
> ... later ...
>
> .set("listname","someName");
>
> and have $list be a pointer to list in the first statement.
>
> Possible?
>
> -Ted.
>
>
> --
> Ted Husted,
>    Junit in Action  - <http://www.manning.com/massol/>,
>    Struts in Action - <http://husted.com/struts/book.html>,
>    JSP Site Design  - <http://www.amazon.com/exec/obidos/ISBN=1861005512>.
>
>
>
>
> ---------------------------------------------------------------------
> 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: Indirect references

Posted by Claude Brisson <cl...@savoirweb.com>.
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"))

CloD

----- Original Message -----
From: "Ted Husted" <hu...@apache.org>
To: <ve...@jakarta.apache.org>
Sent: vendredi 8 août 2003 20:11
Subject: Indirect references


> This may sound odd, but, if it were possible, I'd like to do a bit of
> indirection in my variable references.
>
> The use case is this: I have a list in the context. The list may end up
> under one name or another, but I do know what the list's name is before
> I forward to the template. I'd like the template to set a local variable
> to the list based on the name that I pass inside of another variable.
>
> Essentially, something like this:
>
> #set($list = ${$listname})
>
> where I've already something like has already been done (in one place or
> another):
>
> .set("someName",list);
>
> ... later ...
>
> .set("listname","someName");
>
> and have $list be a pointer to list in the first statement.
>
> Possible?
>
> -Ted.
>
>
> --
> Ted Husted,
>    Junit in Action  - <http://www.manning.com/massol/>,
>    Struts in Action - <http://husted.com/struts/book.html>,
>    JSP Site Design  - <http://www.amazon.com/exec/obidos/ISBN=1861005512>.
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


Re: Indirect references

Posted by Nathan Bubna <na...@esha.com>.
Ted Husted said:
> This may sound odd, but, if it were possible, I'd like to do a bit of
> indirection in my variable references.

how odd!  :)

the RenderTool or its cohort ViewRenderTool in the velocity-tools project may
be of assistance here.

Nathan Bubna
nathan@esha.com


Re: Indirect references

Posted by Nathan Bubna <na...@esha.com>.
Ted Husted said:
> This may sound odd, but, if it were possible, I'd like to do a bit of
> indirection in my variable references.

how odd!  :)

the RenderTool or its cohort ViewRenderTool in the velocity-tools project may
be of assistance here.

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