Skip to content

Instantly share code, notes, and snippets.

@davelnewton
Created October 5, 2011 03:46
Show Gist options
  • Save davelnewton/1263568 to your computer and use it in GitHub Desktop.
Save davelnewton/1263568 to your computer and use it in GitHub Desktop.
Velocity array access
package main;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import java.io.StringWriter;
public class Main {
public static void main(String[] args) {
Velocity.init();
VelocityContext context = new VelocityContext();
context.put("name", "Velocity");
String[] foo = { "hi", "there" };
context.put("foo", foo);
try {
Template template = Velocity.getTemplate("template.vm");
StringWriter sw = new StringWriter();
template.merge(context, sw);
System.out.println(sw.toString());
} catch (ResourceNotFoundException e) {
e.printStackTrace();
} catch (ParseErrorException e) {
e.printStackTrace();
} catch (MethodInvocationException e) {
e.printStackTrace();
}
}
}
Hi, Velocity.
hi
there
Hi, $name.
$foo[0]
$foo[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment