2016-12-19 11 views
0

沿模板我想创建一个车把java的模板是这样的:循环在Java车把与其他参数

"This is a sample template with {{parameter1}} {{#if object_list}} {{#each object_list}} {{object_list.somevar}} {{object_list.othervar}} {{/each}}{{/if}}" 

之前,我可以做object_list.something,我甚至无法做一个简单的循环。我试过如下:

Map<String, String> map = new HashMap<String, String>(); 
    map.put("people", "[ Yehuda Katz, Alan Johnson, Charles Jolley ]"); 
    map.put("k2", "v2"); 

     System.out.println("Map: " 
      + handlebars.compileInline("{{#each people}} {{@index}}:{{this}} \n {{/each}}") 
      .apply(map));    
} 

,它给:

Map: :[[email protected] 
    :false 

就如何实现这一目标的任何指针? 注意:这些参数将在JSON文件接收,所以我不能(或将不喜欢)实际创建的对象的列表。

我下面:https://github.com/jknack/handlebars.java

回答

0

可以与底座胡子做。按预期工作。以下是一个例子:

private static void test2() throws IOException { 
     HashMap<String, Object> scopes = new HashMap<String, Object>(); 
     scopes.put("name", "Mustache"); 

     List<String> features = new ArrayList<String>(); 
     features.add("f1"); 
     features.add("f2"); 

     scopes.put("features", features); 

     List<Map<String, String>> discounts = new ArrayList<Map<String,String>>(); 
     Map<String, String> discount1 = new HashMap<String, String>(); 
     Map<String, String> discount2 = new HashMap<String, String>(); 
     discount1.put("type", "a"); 
     discount1.put("value", "15"); 

     discount2.put("type", "b"); 
     discount2.put("value", "215"); 

     discounts.add(discount1); 
     discounts.add(discount2); 

     scopes.put("discounts", discounts); 

     Writer writer = new OutputStreamWriter(System.out); 
     MustacheFactory mf = new DefaultMustacheFactory(); 
     Mustache mustache = mf.compile(new StringReader("{{name}}, {{#features}} Feature: {{.}} {{/features}}! {{#discounts}} {{type}} {{value}}{{/discounts}}"), "example"); 
     mustache.execute(writer, scopes); 
     writer.flush(); 
}