I was trying to get Velocity working stand alone the other night and had a bit of trouble with the template locations. Got it working by passing in a
file.resource.loader.path property to the init method.Java code...
public class App
{
public static void main( String[] args )
{
try {
VelocityEngine ve = new VelocityEngine();
Properties properties = new Properties();
properties.setProperty("file.resource.loader.path", "/home/footech");
ve.init(properties);
ListmyList = new ArrayList ();
myList.add("one");
myList.add("two");
myList.add("three");
VelocityContext context = new VelocityContext();
context.put("aList", myList);
Template t = ve.getTemplate("footech.vm");
StringWriter writer = new StringWriter();
t.merge(context, writer);
System.out.println(writer.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Put a footech.vm template in /home/footech.
Template example...
Hello.
#foreach($number in $aList)
The number is $number
#end
1 comments:
To use a String instead of file as a template:
ve.evaluate(context,writer,"LOG", "This is a String"));
Javadoc at:
http://velocity.apache.org/engine/devel/apidocs/org/apache/velocity/app/Velocity.html#evaluate(org.apache.velocity.context.Context, java.io.Writer, java.lang.String, java.lang.String)
Post a Comment