Reference a Map by name within Velocity Template
- by Wiretap
Pretty sure there is an easy answer to this, but just can't find the right VTL syntax.
In my context I'm passing a Map which contains other Maps. I'd like to reference these inner maps by name and assign them within my template. The inner maps are constructed by different parts of the app, and then added to the context
by way of example
public static void main( String[] args )
throws Exception
{
VelocityEngine ve = new VelocityEngine();
ve.init();
Template t = ve.getTemplate( "test.vm" );
VelocityContext context = new VelocityContext();
Map<String,Map<String,String>> messageData = new HashMap<String, Map<String,String>>();
Map<String,String> data_map = new HashMap<String,String>();
data_map.put("data_1","1234");
data_map.put("a_date", "31-Dec-2009");
messageData.put("inner_map", data_map);
context.put("msgData", messageData);
StringWriter writer = new StringWriter();
t.merge( context, writer );
System.out.println( writer.toString() );
}
Template - test.vm
#set ($in_map = $msgData.get($inner_map) )
data:
$in_map.data_1
$in_map.a_date