Search in sources :

Example 1 with Template

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

use of org.beetl.core.Template in project docx4j-template by vindell.

the class WordprocessingMLBeetlTemplate method process.

@Override
public WordprocessingMLPackage process(String template, Map<String, Object> variables) throws Exception {
    // 使用Beetl模板引擎渲染模板
    Template beeTemplate = getEngine().getTemplate(template);
    beeTemplate.binding(variables);
    // 获取模板渲染后的结果
    String html = beeTemplate.render();
    // 使用HtmlTemplate进行渲染
    return mlHtmlTemplate.process(html, variables);
}
Also used : WordprocessingMLHtmlTemplate(org.docx4j.template.WordprocessingMLHtmlTemplate) GroupTemplate(org.beetl.core.GroupTemplate) Template(org.beetl.core.Template) WordprocessingMLTemplate(org.docx4j.template.WordprocessingMLTemplate)

Example 3 with Template

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

the class FileFunctionWrapper method call.

@Override
public Object call(Object[] paras, Context ctx) {
    try {
        Template template = ctx.gt.getHtmlFunctionOrTagTemplate(this.resourceId);
        // 能显示异常
        template.isRoot = false;
        template.binding(ctx.globalVar);
        for (int i = 0; i < paras.length; i++) {
            template.binding("para".concat(String.valueOf(i)), paras[i]);
        }
        template.renderTo(ctx.byteWriter);
        Object[] vars = template.getCtx().vars;
        Object o = vars[vars.length - 1];
        if (o != Context.NOT_EXIST_OBJECT) {
            return o;
        } else {
            return null;
        }
    } catch (BeetlException ex) {
        throw ex;
    } catch (Exception ex) {
        BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "调用方法出错 " + this.resourceId, ex);
        throw be;
    }
}
Also used : BeetlException(org.beetl.core.exception.BeetlException) BeetlException(org.beetl.core.exception.BeetlException) Template(org.beetl.core.Template)

Example 4 with Template

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

Example 5 with Template

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

the class PairDLTest method testPair.

@Test
public void testPair() throws Exception {
    GroupTemplate newGt = getGt();
    Template t = newGt.getTemplate("/text/pair_template.html");
    String str = t.render();
    AssertJUnit.assertEquals(this.getFileContent("/text/pair_expected.html"), str);
}
Also used : GroupTemplate(org.beetl.core.GroupTemplate) 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