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);
}
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);
}
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;
}
}
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());
}
}
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);
}
Aggregations