jmte 是一个简单,但是高效的模版引擎,graylog 对于通知的处理就使用了此模版引擎,使用简单,而且比较灵活
参考使用
- 流程
创建引擎,注册类型渲染(不是必须的),使用tranform 进行渲染处理 - 参考代码MyRenderDemo
MyRenderDemo
public class MyRenderDemo {
static Engine engine ;
static {
engine = Engine.createEngine();
engine.registerRenderer(User.class,new MyRender());
}
public static <T> String render(String template, Map<String,Object> context) {
return engine.transform(template,context);
}
}
MyRender
public class MyRender implements Renderer<User> {
@Override
public String render(User o, Locale locale, Map model) {
return String.format("%d----------%s------%s",o.getAge(),o.getName(),o.getExtend());
}
}
main 入口
String template = "======= ";
String template2 = "{\n" +
"\"nane\":\"\",\n" +
"\"age\":,\n" +
" \"extend\":\"\"\n" +
"}";
String template3 ="\n" +
" <em></em>\n" +
" <em></em>\n" +
"";
Map<String,Object> extendMsg = new HashMap<>();
extendMsg.put("type","local message");
Map<String,Object> context = new HashMap<>();
User user = new User();
user.setExtend(extendMsg);
user.setAge(333);
user.setConf(Arrays.asList(new Conf("ddd","333"),new Conf("rrrr","5555")));
user.setName("33333");
context.put("user",user);
String result = MyRenderDemo.render(template,context);
System.out.println(result);
String result2 = MyRenderDemo.render(template2,context);
System.out.println(result2);
String result3 = MyRenderDemo.render(template3,context);
System.out.println(result3);
- 效果
说明
jmte 使用简单,支持的基本逻辑操作还是不少的,比如if foreach, 而且支持比较强大的自定义处理,对于cms 系统或者营销系统来说都是不错的选择
参考资料
https://www.tinymediamanager.org/docs/jmte
https://github.com/DJCordhose/jmte