com.power.doc.utils.BeetlTemplateUtil Maven / Gradle / Ivy
package com.power.doc.utils;
import com.power.common.util.FileUtil;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 获取模板
*
* @author sunyu on 2016/12/6.
*/
public class BeetlTemplateUtil {
public static Template getByName(String templateName) {
try {
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("/template/");
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
return gt.getTemplate(templateName);
} catch (IOException e) {
throw new RuntimeException("获取模板异常");
}
}
/**
*
* @param path path
* @param params params
* @return map
*/
public static Map getTemplatesRendered(String path, Map params) {
Map templateMap = new HashMap<>();
File[] files = FileUtil.getResourceFolderFiles(path);
GroupTemplate gt = getGroupTemplate(path);
for (File f : files) {
if (f.isFile()) {
String fileName = f.getName();
Template tp = gt.getTemplate(fileName);
if (null != params) {
tp.binding(params);
}
templateMap.put(fileName, tp.render());
}
}
return templateMap;
}
/**
*
* @param path
* @return
*/
private static GroupTemplate getGroupTemplate(String path) {
try {
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("/"+path+"/");
Configuration cfg = Configuration.defaultConfiguration();
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
return gt;
} catch (IOException e) {
throw new RuntimeException("获取模板异常");
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy