use of org.beetl.core.resource.StringTemplateResourceLoader in project hutool by looly.
the class BeetlUtilTest method renderStrTest.
@Test
public void renderStrTest() throws IOException {
GroupTemplate groupTemplate = BeetlUtil.createGroupTemplate(new StringTemplateResourceLoader(), Configuration.defaultConfiguration());
Template template = BeetlUtil.getTemplate(groupTemplate, "hello,${name}");
String result = BeetlUtil.render(template, Dict.create().set("name", "hutool"));
Assert.assertEquals("hello,hutool", result);
String renderFromStr = BeetlUtil.renderFromStr("hello,${name}", Dict.create().set("name", "hutool"));
Assert.assertEquals("hello,hutool", renderFromStr);
}
use of org.beetl.core.resource.StringTemplateResourceLoader in project beetl2.0 by javamonkey.
the class VirtualAttributeTest method testJacksonVirtualAttribute.
@Test
public void testJacksonVirtualAttribute() throws Exception {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\":\"lijz\",\"id\":10}";
JsonNode node = mapper.readTree(json);
gt.registerVirtualAttributeEval(new VirtualAttributeEval() {
@Override
public Object eval(Object o, String attributeName, Context ctx) {
JsonNode node = (JsonNode) o;
return node.get(attributeName);
}
@Override
public boolean isSupport(Class c, String attributeName) {
if (JsonNode.class.isAssignableFrom(c))
return true;
else
return false;
}
});
// String str = "${json.name}"; jaskson 支持get方法,因此不用虚拟属性也能访问
String str = "${json.~name}";
Template template = gt.getTemplate(str, new StringTemplateResourceLoader());
template.binding("json", node);
String ret = template.render();
AssertJUnit.assertEquals("\"lijz\"", ret);
}
use of org.beetl.core.resource.StringTemplateResourceLoader in project beetl2.0 by javamonkey.
the class HelloBeetl method main.
public static void main(String[] args) throws Exception {
StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Template t = gt.getTemplate("hello,${name}");
t.binding("name", "beetl");
String str = t.render();
System.out.println(str);
}
use of org.beetl.core.resource.StringTemplateResourceLoader in project hello-world by haoziapple.
the class BasicUse method main.
public static void main(String[] args) throws Exception {
StringTemplateResourceLoader resourceLoader = new StringTemplateResourceLoader();
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Template t = gt.getTemplate("hello,${name}");
t.binding("name", "beetl");
String str = t.render();
System.out.println(str);
}
use of org.beetl.core.resource.StringTemplateResourceLoader in project beetl2.0 by javamonkey.
the class VirtualAttributeTest method testFastjosnVirtualAttribute.
@Test
public void testFastjosnVirtualAttribute() throws Exception {
String json = "{\"name\":\"lijz\",\"id\":10}";
JSONObject node = (JSONObject) JSON.parse(json);
gt.registerVirtualAttributeEval(new VirtualAttributeEval() {
@Override
public Object eval(Object o, String attributeName, Context ctx) {
return ((JSONObject) o).get(attributeName);
}
@Override
public boolean isSupport(Class c, String attributeName) {
if (c == JSONObject.class || c == JSONArray.class) {
return true;
} else {
return false;
}
}
});
String str = "${json.~name}";
Template template = gt.getTemplate(str, new StringTemplateResourceLoader());
template.binding("json", node);
String ret = template.render();
AssertJUnit.assertEquals("lijz", ret);
}
Aggregations