Search in sources :

Example 6 with Context

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

use of org.beetl.core.Context 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 8 with Context

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

the class EmptyFunction method main.

public static void main(String[] args) {
    EmptyFunction fn = new EmptyFunction();
    Context ctx = new Context();
    ctx.set("list", new ArrayList());
    ctx.set("array", new Object[] {});
    ctx.set("array1", new Object[] { 1, 2 });
    System.out.println(fn.call(new Object[] { "lijz" }, ctx));
    System.out.println(fn.call(new Object[] { "list" }, ctx));
    System.out.println(fn.call(new Object[] { "array" }, ctx));
    System.out.println(fn.call(new Object[] { "array1" }, ctx));
}
Also used : Context(org.beetl.core.Context) ArrayList(java.util.ArrayList)

Aggregations

Context (org.beetl.core.Context)8 Template (org.beetl.core.Template)5 Test (org.testng.annotations.Test)5 JSONObject (com.alibaba.fastjson.JSONObject)4 VirtualAttributeEval (org.beetl.core.VirtualAttributeEval)3 List (java.util.List)2 StringTemplateResourceLoader (org.beetl.core.resource.StringTemplateResourceLoader)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ArrayList (java.util.ArrayList)1 Function (org.beetl.core.Function)1 User (org.beetl.core.User)1 VirtualClassAttribute (org.beetl.core.VirtualClassAttribute)1