Search in sources :

Example 1 with StringTemplateResourceLoader

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);
}
Also used : GroupTemplate(org.beetl.core.GroupTemplate) StringTemplateResourceLoader(org.beetl.core.resource.StringTemplateResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template) Test(org.junit.Test)

Example 2 with StringTemplateResourceLoader

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);
}
Also used : Context(org.beetl.core.Context) JsonNode(com.fasterxml.jackson.databind.JsonNode) VirtualAttributeEval(org.beetl.core.VirtualAttributeEval) JSONObject(com.alibaba.fastjson.JSONObject) StringTemplateResourceLoader(org.beetl.core.resource.StringTemplateResourceLoader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 3 with StringTemplateResourceLoader

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);
}
Also used : Configuration(org.beetl.core.Configuration) StringTemplateResourceLoader(org.beetl.core.resource.StringTemplateResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 4 with StringTemplateResourceLoader

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);
}
Also used : Configuration(org.beetl.core.Configuration) StringTemplateResourceLoader(org.beetl.core.resource.StringTemplateResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 5 with StringTemplateResourceLoader

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);
}
Also used : Context(org.beetl.core.Context) JSONObject(com.alibaba.fastjson.JSONObject) VirtualAttributeEval(org.beetl.core.VirtualAttributeEval) JSONObject(com.alibaba.fastjson.JSONObject) StringTemplateResourceLoader(org.beetl.core.resource.StringTemplateResourceLoader) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Aggregations

Template (org.beetl.core.Template)6 StringTemplateResourceLoader (org.beetl.core.resource.StringTemplateResourceLoader)6 GroupTemplate (org.beetl.core.GroupTemplate)4 Configuration (org.beetl.core.Configuration)3 Test (org.testng.annotations.Test)3 JSONObject (com.alibaba.fastjson.JSONObject)2 Context (org.beetl.core.Context)2 VirtualAttributeEval (org.beetl.core.VirtualAttributeEval)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Test (org.junit.Test)1