velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nathan Bubna" <nbu...@gmail.com>
Subject Re: creating references to arbitrary hashtable
Date Mon, 27 Oct 2008 16:55:05 GMT
The VelocityLayoutServlet comes with support for VelocityTools which
does automatically instantiate "tools" and populate the context with
them.

You can have it automatically create a request-scoped Hashtable for
you with a toolbox.xml like this (in VelocityTools 1.4):

<toolbox>
  <tool>
    <key>slots</key>
    <scope>request</scope>
    <class>java.util.Hashtable</class>
  </tool>
</toolbox>

or a tools.xml like this (in VelocityTools 2.0):

<tools>
  <toolbox scope="request">
    <tool key="slots" class="java.util.Hashtable"/>
  </toolbox>
</tools>

Of course, you can also create maps dynamically from within your
template like this:

#set( $slots = { 'foo':$foo, 'x':'y', 'bar':1.0 } )

and even modify that map later like this:

#set( $slots.foo = false )

On Mon, Oct 27, 2008 at 9:38 AM,  <Peter_Ford@blm.gov> wrote:
> In the default case the VelocityContext starts off empty. Anything you need
> prior to merging a template should be put in place using
> VelocityContext.put():
>
> context.put("slots", new HashMap());
>
> (incidentally, I'd use HashMap rather than Hashtable - it's more
> efficient.)
>
> In the servlet case I generally use VelocityViewServlet rather than
> VelocityLayoutServlet, but I assume they're similar in allowing you to get
> to the request, session and application attributes in much the same way; in
> that case you'd use request.setAttribute("slots", new HashMap()) in your
> business logic.
>
> To the best of my knowledge, Velocity doesn't automatically instantiate
> anything - you have to do that before merging, or use #set() inside a
> template.
>
> --Pete
>
> "Robert Huffstedtler" <rhuffstedtler@ironworks.com> wrote on 10/27/2008
> 09:54:54 AM:
>
>> It's not yet.  That's the implication I was trying to figure out in the
>> docs.  It kind of seems like the docs imply that a hashtable would
>> automatically be instantiated for the identifier.  However, if that's
>> not the case, am I correct that I just need to instantiate a new
>> java.util.Hashtable and put it into the context as "slots"?
>>
>> If that's the case, it raises the next issue that is not immediately
>> obvious to me from the docs - is there any way by configuration to tell
>> VelocityLayoutServlet which objects to instantiate and what identifiers
>> to associate with them, or do I need to subclass the layout servlet and
>> add it to the context programmatically?
>>
>> Thanks,
>> Rob
>>
>> -----Original Message-----
>> From: Peter_Ford@blm.gov [mailto:Peter_Ford@blm.gov]
>> Sent: Monday, October 27, 2008 11:34 AM
>> To: Velocity Users List
>> Subject: Re: creating references to arbitrary hashtable
>>
>> If "slots" is a Map object, try:
>>
>> $slots.get("foo")
>>
>>
>> "Robert Huffstedtler" <rhuffstedtler@ironworks.com> wrote on 10/27/2008
>> 09:27:26 AM:
>>
>> > Hi folks -
>> >
>> >
>> >
>> > I am helping a group migrate some content from Coldfusion to Velocity.
>> > In their CF code, they have a custom tag which they use in individual
>> > pages for grouping commonly used page variables into a pseudo-object
>> > before passing it off to the page wrapper.  The model is very similar
>> to
>> > the use of the VelocityLayoutServlet, so that's the direction I'm
>> going
>> > for the migration.
>> >
>> >
>> >
>> > However, trying to set properties on a VTL identifier does not seem to
>> > work if the identifier does not reference an object.  The
>> documentation
>> > does not make it clear that that is a requirement
>> >
>> (http://velocity.apache.org/engine/releases/velocity-1.5/user-guide.html
>> > #references).  The description of properties implies that one can
>> > associate a VTL identifier with a hashtable, but there aren't clear
>> > directions for doing so.
>> >
>> >
>> >
>> > Putting this -
>> >
>> >
>> >
>> > #set ( $slots.foo = "Foo")
>> >
>> >
>> >
>> > In my index.vm and referencing it like this in my Default.vm:
>> >
>> >
>> >
>> > $slots.foo<br/>
>> >
>> >
>> >
>> > Is just resulting in $slots.foo being output as a literal.
>> >
>> >
>> >
>> > Can someone point me in the right direction here?
>> >
>> >
>> >
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

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


Mime
View raw message