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