Search in sources :

Example 76 with Template

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

the class SimpleVATest method testFunctionAttribute.

@Test
public void testFunctionAttribute() throws Exception {
    gt.registerFunction("userArray", new Function() {

        @Override
        public Object call(Object[] paras, Context ctx) {
            // TODO Auto-generated method stub
            return new Object[] { User.getTestUser(), "a" };
        }
    });
    Template t = gt.getTemplate("/va/va_fun_template.html");
    String str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/va/va_fun_expected.html"), str);
    t = gt.getTemplate("/va/va_fun_template.html");
    str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/va/va_fun_expected.html"), str);
}
Also used : Context(org.beetl.core.Context) Function(org.beetl.core.Function) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 77 with Template

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

Example 78 with Template

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

the class ResourceLoaderTest method testExtraLoader2.

@Test
public void testExtraLoader2() throws Exception {
    Map data = getData();
    MapResourceLoader strLoader = new MapResourceLoader(data);
    Template t = gt.getTemplate("db:2", strLoader);
    String result = t.render();
    AssertJUnit.assertEquals("hello", result);
}
Also used : Map(java.util.Map) HashMap(java.util.HashMap) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 79 with Template

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

the class HtmlTagTest method testHolderInAttr.

@Test
public void testHolderInAttr() throws Exception {
    Template t = gt.getTemplate("/tag/html8_template.html");
    t.binding("a", 1);
    String str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/tag/html8_expected.html"), str);
    t = gt.getTemplate("/tag/html8_template.html");
    t.binding("a", 1);
    str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/tag/html8_expected.html"), str);
}
Also used : GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

Example 80 with Template

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

the class HtmlTagTest method testExpTag.

@Test
public void testExpTag() throws Exception {
    Template t = gt.getTemplate("/tag/html6_template.html");
    String str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/tag/html6_expected.html"), str);
    t = gt.getTemplate("/tag/html6_template.html");
    str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/tag/html6_expected.html"), str);
}
Also used : GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template) Test(org.testng.annotations.Test)

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