Search in sources :

Example 1 with GroupTemplate

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

use of org.beetl.core.GroupTemplate in project nutzboot by nutzam.

the class BeetlViewMakerStarter method init.

public void init() throws IOException {
    if (appContext == null)
        return;
    log.debug("beetl init ....");
    groupTemplate = appContext.getIoc().get(GroupTemplate.class);
    render = new WebRender(groupTemplate);
}
Also used : WebRender(org.beetl.ext.web.WebRender) GroupTemplate(org.beetl.core.GroupTemplate)

Example 3 with GroupTemplate

use of org.beetl.core.GroupTemplate in project vip by guangdada.

the class GunsTemplateEngine method initBeetlEngine.

public void initBeetlEngine() {
    Properties properties = new Properties();
    properties.put("RESOURCE.root", "");
    properties.put("DELIMITER_STATEMENT_START", "<%");
    properties.put("DELIMITER_STATEMENT_END", "%>");
    properties.put("HTML_TAG_FLAG", "##");
    Configuration cfg = null;
    try {
        cfg = new Configuration(properties);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
    groupTemplate = new GroupTemplate(resourceLoader, cfg);
    groupTemplate.registerFunctionPackage("tool", new ToolUtil());
}
Also used : ToolUtil(com.ikoori.vip.server.core.util.ToolUtil) Configuration(org.beetl.core.Configuration) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) IOException(java.io.IOException) GroupTemplate(org.beetl.core.GroupTemplate) Properties(java.util.Properties)

Example 4 with GroupTemplate

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

the class VarAttributeNodeListener method onEvent.

@Override
public Object onEvent(Event e) {
    Stack stack = (Stack) e.getEventTaget();
    Object o = stack.peek();
    if (o instanceof VarRef) {
        VarRef ref = (VarRef) o;
        VarAttribute[] attrs = ref.attributes;
        for (int i = 0; i < attrs.length; i++) {
            GroupTemplate gt = (GroupTemplate) ((Map) stack.get(0)).get("groupTemplate");
            VarAttribute attr = attrs[i];
            if (attr.getClass() == VarAttribute.class) {
                Type type = attr.type;
                String name = attr.token != null ? attr.token.text : null;
                // 换成速度较快的属性访问类
                try {
                    AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(type.cls, name, gt);
                    attr.aa = aa;
                } catch (BeetlException ex) {
                    ex.pushToken(attr.token);
                    throw ex;
                }
            } else if (attr.getClass() == VarSquareAttribute.class) {
                Type type = attr.type;
                Class c = type.cls;
                if (Map.class.isAssignableFrom(c)) {
                    attr.setAA(gt.getAttributeAccessFactory().getMapAA());
                } else if (List.class.isAssignableFrom(c) || Set.class.isAssignableFrom(c)) {
                    attr.setAA(gt.getAttributeAccessFactory().getListAA());
                } else if (c.isArray()) {
                    attr.setAA(gt.getAttributeAccessFactory().getArrayAA());
                } else {
                    Expression exp = ((VarSquareAttribute) attr).exp;
                    if (exp instanceof Literal) {
                        Literal literal = (Literal) exp;
                        if (literal.obj instanceof String) {
                            try {
                                String attributeName = (String) literal.obj;
                                AttributeAccess aa = gt.getAttributeAccessFactory().buildFiledAccessor(c, attributeName, gt);
                                ref.attributes[i] = new VarSquareAttribute2((VarSquareAttribute) attrs[i], attributeName, aa);
                            } catch (BeetlException ex) {
                                ex.pushToken(attr.token);
                                throw ex;
                            }
                        }
                    }
                }
            } else if (attr.getClass() == VarVirtualAttribute.class) {
                // 对虚拟属性~size做优化
                if (attr.token.text.equals("size")) {
                    // 优化
                    Class c = attr.type.cls;
                    if (Map.class.isAssignableFrom(c)) {
                        ref.attributes[i] = new MapSizeVirtualAttribute((VarVirtualAttribute) attr);
                    } else if (Collection.class.isAssignableFrom(c)) {
                        ref.attributes[i] = new CollectionSizeVirtualAttribute((VarVirtualAttribute) attr);
                    } else if (c.isArray()) {
                        ref.attributes[i] = new ArraySizeVirtualAttribute((VarVirtualAttribute) attr);
                    }
                }
            }
        }
    }
    return null;
}
Also used : VarRef(org.beetl.core.statement.VarRef) VarVirtualAttribute(org.beetl.core.statement.VarVirtualAttribute) BeetlException(org.beetl.core.exception.BeetlException) VarSquareAttribute(org.beetl.core.statement.VarSquareAttribute) Set(java.util.Set) GroupTemplate(org.beetl.core.GroupTemplate) AttributeAccess(org.beetl.core.om.AttributeAccess) Stack(java.util.Stack) Type(org.beetl.core.statement.Type) Expression(org.beetl.core.statement.Expression) VarAttribute(org.beetl.core.statement.VarAttribute) Literal(org.beetl.core.statement.Literal) Collection(java.util.Collection) List(java.util.List) Map(java.util.Map)

Example 5 with GroupTemplate

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

the class Test method main.

public static void main(String[] args) throws Exception {
    ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("org/beetl/core/lab/");
    Configuration cfg = Configuration.defaultConfiguration();
    cfg.setDirectByteOutput(true);
    cfg.getResourceMap().put("tagRoot", "/");
    cfg.getPkgList().add("org.beetl.core.lab.");
    GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
    // cfg.setStatementStart("@");
    // cfg.setStatementEnd(null);
    // cfg.setPlaceholderStart("{{");
    // cfg.setPlaceholderEnd("}}");
    // 
    gt.registerFunction("test", new TestFun());
    gt.registerTag("test", TestTag.class);
    List list = new ArrayList();
    list.add(null);
    list.add(new TestUser("abc"));
    HashMap map = new HashMap();
    map.put("key", 123);
    gt.enableStrict();
    for (int i = 0; i < 1; i++) {
        Template t = gt.getTemplate("/hello.txt");
        t.binding("user", new TestUser("jo"));
        t.binding("id", 2);
        t.binding("list", list);
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        try {
            t.renderTo(bs);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        // TestUser test = new TestUser("a");
        // test.setLover(new TestUser("b"));
        // t.binding("user", test);
        System.out.println(t.render());
    }
}
Also used : Configuration(org.beetl.core.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ClasspathResourceLoader(org.beetl.core.resource.ClasspathResourceLoader) List(java.util.List) ArrayList(java.util.ArrayList) GroupTemplate(org.beetl.core.GroupTemplate) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template)

Aggregations

GroupTemplate (org.beetl.core.GroupTemplate)31 Configuration (org.beetl.core.Configuration)22 Template (org.beetl.core.Template)19 ClasspathResourceLoader (org.beetl.core.resource.ClasspathResourceLoader)10 FileResourceLoader (org.beetl.core.resource.FileResourceLoader)7 IOException (java.io.IOException)5 Test (org.testng.annotations.Test)5 Map (java.util.Map)4 Properties (java.util.Properties)4 StringTemplateResourceLoader (org.beetl.core.resource.StringTemplateResourceLoader)4 WebRender (org.beetl.ext.web.WebRender)4 HashMap (java.util.HashMap)3 InputStream (java.io.InputStream)2 List (java.util.List)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 WebAppResourceLoader (org.beetl.core.resource.WebAppResourceLoader)2 ToolUtil (com.ikoori.vip.server.core.util.ToolUtil)1 ActionContext (com.opensymphony.xwork2.ActionContext)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1