beetl模板入门例子
beetl初级用法
public class BeetlEngine {
public static String render(String template,String varName,Object varObject){
StringTemplateResourceLoader resourceLoader =new StringTemplateResourceLoader();
Configuration cfg =null;
try {
cfg =Configuration.defaultConfiguration();
}catch (IOException e) {
//log error
}
GroupTemplate gt =new GroupTemplate(resourceLoader, cfg);
Template t =gt.getTemplate(template);
t.binding(varName, varObject);
return t.render();;
}
public static void main(String[] args) {
//假设Member是一个对象,有telephone和userName两个字段
Member member =new Member();
member.setTelephone("188");
member.setUserName("yes");
String str =render("hello,${member.userName}, tel:${member.telephone}","member",member);
System.out.println(str);// 输出hello,yes, tel:188
//以下是map用法
Map m =new HashMap<>();
m.put("userName","test");
m.put("telephone",133);
String str2 =render("hello,${m.userName}, tel:${m.telephone}","m",m);
System.out.println(str2);// 输出:hello,test, tel:133
}
}