Search in sources :

Example 11 with Template

use of org.beetl.core.Template 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 12 with Template

use of org.beetl.core.Template in project beetl2.0 by javamonkey.

the class MetaCopyTest method main.

public static void main(String[] args) throws Exception {
    String home = System.getProperty("user.dir") + File.separator + "template" + File.separator;
    Configuration cf = Configuration.defaultConfiguration();
    FileResourceLoader rs = new FileResourceLoader(home, cf.getCharset());
    GroupTemplate gt = new GroupTemplate(rs, cf);
    Template t = gt.getTemplate("/helloworld.html");
    Program p = gt.getProgram("/helloworld.html");
    ProgramMetaData old = p.metaData;
    ProgramMetaData copy = old.copy();
    System.out.println("ok");
}
Also used : Program(org.beetl.core.statement.Program) Configuration(org.beetl.core.Configuration) ProgramMetaData(org.beetl.core.statement.ProgramMetaData) FileResourceLoader(org.beetl.core.resource.FileResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 13 with Template

use of org.beetl.core.Template in project beetl2.0 by javamonkey.

the class ClasspathRL method main.

public static void main(String[] args) throws Exception {
    ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
    URL url = resourceLoader.getClass().getResource("/org/beetl/sample/s01/hello.txt");
    Configuration cfg = Configuration.defaultConfiguration();
    GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
    Template t = gt.getTemplate("/org/beetl/sample/s01/hello.txt");
    String str = t.render();
    System.out.println(str);
}
Also used : Configuration(org.beetl.core.Configuration) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) URL(java.net.URL) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 14 with Template

use of org.beetl.core.Template in project beetl2.0 by javamonkey.

the class FilePathRL method main.

public static void main(String[] args) throws Exception {
    String root = System.getProperty("user.dir") + File.separator + "template";
    FileResourceLoader resourceLoader = new FileResourceLoader(root, "utf-8");
    Configuration cfg = Configuration.defaultConfiguration();
    GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
    Template t = gt.getTemplate("/s01/hello.txt");
    String str = t.render();
    System.out.println(str);
}
Also used : Configuration(org.beetl.core.Configuration) FileResourceLoader(org.beetl.core.resource.FileResourceLoader) GroupTemplate(org.beetl.core.GroupTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Example 15 with Template

use of org.beetl.core.Template 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)

Aggregations

Template (org.beetl.core.Template)122 Test (org.testng.annotations.Test)96 GroupTemplate (org.beetl.core.GroupTemplate)41 Map (java.util.Map)15 User (org.beetl.core.User)14 HashMap (java.util.HashMap)13 Configuration (org.beetl.core.Configuration)13 FileResourceLoader (org.beetl.core.resource.FileResourceLoader)6 StringTemplateResourceLoader (org.beetl.core.resource.StringTemplateResourceLoader)6 Context (org.beetl.core.Context)5 JSONObject (com.alibaba.fastjson.JSONObject)4 List (java.util.List)4 ClasspathResourceLoader (org.beetl.core.resource.ClasspathResourceLoader)4 VirtualAttributeEval (org.beetl.core.VirtualAttributeEval)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 BodyContent (org.beetl.core.BodyContent)2 BeetlException (org.beetl.core.exception.BeetlException)2 WebRender (org.beetl.ext.web.WebRender)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1