public void count( Counter counter, int times, String name, Map<String, Object> context, Writer writer, Writer discardWriter, OutputStream discardStream) throws Exception { counter.beginning(); GroupTemplate group = new GroupTemplate(); group.enableOptimize(); group.enableNativeCall(); counter.initialized(); Template template = group.getReaderTemplate( new InputStreamReader( BeetlCase.class.getClassLoader().getResourceAsStream("performance/books.btl"))); for (Map.Entry<String, Object> entry : context.entrySet()) { template.set(entry.getKey(), entry.getValue()); } counter.compiled(); template.getText(writer); counter.executed(); if (discardStream != null) { for (int i = times; i >= 0; i--) { template.getText(discardStream); } } else { for (int i = times; i >= 0; i--) { template.getText(discardWriter); } } counter.finished(); }
@SuppressWarnings("unchecked") @Override protected void renderMergedTemplateModel( Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { BeetlGroupUtilConfiguration config = (BeetlGroupUtilConfiguration) this.getApplicationContext().getBean("beetlConfig"); GroupTemplate group = config.getGroupTemplate(); Template t = group.getFileTemplate(getUrl()); group.enableNativeCall(); Iterator<Entry<String, Object>> it = model.entrySet().iterator(); Entry<String, Object> entry = null; while (it.hasNext()) { entry = it.next(); t.set(entry.getKey(), entry.getValue()); } WebVariable webVariable = new WebVariable(); webVariable.setRequest(request); webVariable.setResponse(response); webVariable.setSession(request.getSession()); t.set("servlet", webVariable); t.set("ctxPath", request.getContextPath()); t.setRawValue("session", new SessionWrapper(request.getSession())); t.getText(response.getWriter()); }
public static void main(String[] args) throws Exception { String home = System.getProperty("user.dir") + File.separator + "template" + File.separator; GroupTemplate group = new GroupTemplate(new File(home)); group.setCharset("GBK"); group.config("<%", "%>", "${", "}"); // group.config("<!--[", "]-->", "<!--[=", "]-->"); group.enableChecker(0.01); group.registerFunction("str2json", new String2Json()); group.setDebug(true); group.enableNativeCall(); // group.enableDirectOutputByte(); Map compileConfig = new HashMap(); compileConfig.put(GroupTemplate.OPTIMIZE_KEEP_SOURCE, true); group.enableOptimize(compileConfig); // group.enableOptimize(); // group.enableDirectOutputByte(); group.registerFunctionPackage("test", new NowFunction()); Template template = group.getFileTemplate("/helloworld.html"); template.set("tt", new Test()); Map map = new HashMap(); map.put("a", "a"); map.put("b", "b"); template.set("map", map); System.out.println(template.getTextAsString()); template = group.getFileTemplate("/helloworld.html"); template.set("tt", new Person()); template.set("map", map); System.out.println(template.getTextAsString()); // template = group.getFileTemplate("/helloworld.html"); // template.set("a", 1); // System.out.println(template.getTextAsString()); // group.removeTemplateCache("hello2"); // Template t = group.getStringTemplate("hello1dfdf2356${name}", "hello2"); // t.set("name", "中文"); // String str = t.getTextAsString(); // System.out.println(str); // }