Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Velocity: hashtable?

Tags:

velocity

The Velocity user guide mentions a "Hashtable". However there's no mentioning how to create one in this language.

So if you could show how to do this -- so that I could write smth. like

#foreach( $key in $foo.keySet() )
    <li>Key: $key -> Value: $foo.get($key)</li>
#end

-- I'd greatly appreciate your help.

Thanks in advance!

// PS: my original problem is : Mechanical Turk / Cmd line tools / Qualification / #set and #foreach in xml So please understand that I am not interested in learning Velocity -- I only need one quick hack if possible. Thanks.

like image 317
Vlad K. Avatar asked Jul 28 '10 22:07

Vlad K.


People also ask

What is Apache Velocity used for?

Apache Velocity first released in April 2001, is a Java-based template engine that provides a template language to reference objects defined in Java code. It aims to ensure clean separation between the presentation tier and business tiers in a Web application (the model–view–controller design pattern).

What is velocity .VM file?

Velocity is a Java-based templating engine. It's an open source web framework designed to be used as a view component in the MVC architecture, and it provides an alternative to some existing technologies such as JSP. Velocity can be used to generate XML files, SQL, PostScript and most other text-based formats.

What is .VM file in Java?

Developer file used by Velocity, a Java-based template engine; written using the Velocity Template Language (VTL); contains VTL statements inserted in a normal text document; often used for auto-generating Web source code and class skeletons.

What is VelocityContext?

VelocityContext(Context innerContext) Chaining constructor, used when you want to wrap a context in another. VelocityContext(Map<String,Object> context) Creates a new instance with the provided storage (and no inner context).


1 Answers

In Velocity you would use the #set directive to create a map. To relate it to your example you might do something like:

#set($foo = {
    "NEWS": "http://news.bbc.com",
    "SEARCH": "http://google.com"
})

Then your foreach example above will do exactly what you need.

like image 96
Will Prescott Avatar answered Sep 30 '22 13:09

Will Prescott