You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Brian Kuhn <bn...@hotmail.com> on 2003/02/25 00:25:41 UTC

using objects created in a template...

Kind of a newbie question, but can anyone point me to an example of how to  
get objects created in a velocity template?  I've found documentation on how 
to do it 'after the merge process is complete', but I have no idea what that 
means.  Can anyone point me to an example of actual code doing this?

Thanks,
     Brian

====================
Brian Kuhn
bnkuhn@hotmail.com
====================



_________________________________________________________________
The new MSN 8: advanced junk mail protection and 2 months FREE*  
http://join.msn.com/?page=features/junkmail


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


Re: using objects created in a template...

Posted by Ch...@dlr.de.
Hi Brian,

it's a bit confusing what you are asking about. I believe
that Tim pretty clearly answered what you *asked*.

Maybe looking it from the other side makes it clearer to you:

The VelocityEngine merges a template with a context you (or
the VelocityViewServlet) provide. This context is in reality
just a java.util.Map.

Anything you put in the context before the merge is available
to the template.

Anything that the template puts in the context is available to
the application after the merge. NOTE: be careful with directives
that overlay chained contexts to the original ones, variables
places there will dissapear after the merge (currently only
a #foreach overlays the index variable).

During template processing, you can return control to your
applications with tools (placed in the context *before* the merge).
These are just class instances with public methods invokeable
from within the template. VelocityViewServlet has a neat pattern
to be able to configure the tools and constants to be used without
having to code the servlet.

So as Tim noted with his <-- MAGIC HAPPENS HERE -->, this
would be either your own implementation of a context. Other
solution would be to invoke a tool of yours to take intermediate
control and add other variables to the context (note that this
tool needs to obtain a reference to the context before the merge
process starts).

A third and most elegant way of achieving what you seem to want
is to put a tool that implements the Map interface into the
context. By putting #set( $yourMapTool.someKey = "foo" ) your
tools "put"-method is called and you can do your magic. Accesing
then $yourMapTool.otherKey returns the magic result you want.

Hope this helpes you to get to speed with velocity,

;) Christoph


Brian Kuhn wrote:>
> Reading further, I noticed that WebMacro has a #param directive that 
> does exactly what I need.  In a WebMacro template you can have the 
> statement
> 
> #param $foo = "bar"
> 
> and in the servlet/app handling the template you can use:
> 
> String bar = template.getParam("foo");
> 
> to get $foo's value.  Velocity doesn't have this functionality?
> 
> -Brian
> 
[snip]
> ----Original Message Follows----
> From: Tim Vernum <Ti...@macquarie.com>
[snip]
> 
> Generally speaking a template is processed in full.
> You say "go" and velocity runs off and processes the whole thing.
> So, by the time you know what "$somekey" is, the template has already
> finished using "$otherkey".
> 
>> From what I can tell you want:
> 
> ----------------------------------
> ##begin template
> 
>   #set ($someKey = "some value")
> 
> ## <-- MAGIC HAPPENS HERE -->
> 
>   #if ($otherKey)
>     new value = $otherKey
>   #end
> 
> ##end template
> ----------------------------------
> 
> So that "something" occurs in the stop that will take the value of
>  $someKey and set $otherKey immediately, so the template can then
>  make use of "$otherKey".
> 
> Two options (that I can see)
> 1] Brute force. Place a tool into the context that does this, and
>  call it when you need to...
> ----------------------------------
> ##begin template
> 
>   #set ($someKey = "some value")
> 
> $keyTool.doStuff()
> 
>   #if ($otherKey)
>     new value = $otherKey
>   #end
> 
> ##end template
> ----------------------------------
> (I think an appropriately scoped tool should be able to make use of
>  the context in the way you need)
> 
> 2] Use your own version of the o.a.v.context.Context interface, that
>  has "triggers" on assignment to certain keys.
> That's probably a fairly nice approach, if the framework you're working
>  with will let you use your own Context implementation.
[snip]



-- 
:) Christoph Reck


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