use of cn.hutool.extra.template.TemplateEngine in project hutool by looly.
the class TemplateFactory method create.
/**
* 根据用户引入的模板引擎jar,自动创建对应的模板引擎对象<br>
* 推荐创建的引擎单例使用,此方法每次调用会返回新的引擎
*
* @param config 模板配置,包括编码、模板文件path等信息
* @return {@link TemplateEngine}
*/
public static TemplateEngine create(TemplateConfig config) {
final TemplateEngine engine = doCreate(config);
StaticLog.debug("Use [{}] Engine As Default.", StrUtil.removeSuffix(engine.getClass().getSimpleName(), "Engine"));
return engine;
}
use of cn.hutool.extra.template.TemplateEngine in project hutool by looly.
the class ThymeleafEngine method createEngine.
/**
* 创建引擎
*
* @param config 模板配置
* @return {@link TemplateEngine}
*/
private static org.thymeleaf.TemplateEngine createEngine(TemplateConfig config) {
if (null == config) {
config = new TemplateConfig();
}
ITemplateResolver resolver;
switch(config.getResourceMode()) {
case CLASSPATH:
final ClassLoaderTemplateResolver classLoaderResolver = new ClassLoaderTemplateResolver();
classLoaderResolver.setCharacterEncoding(config.getCharsetStr());
classLoaderResolver.setTemplateMode(TemplateMode.HTML);
classLoaderResolver.setPrefix(StrUtil.addSuffixIfNot(config.getPath(), "/"));
resolver = classLoaderResolver;
break;
case FILE:
final FileTemplateResolver fileResolver = new FileTemplateResolver();
fileResolver.setCharacterEncoding(config.getCharsetStr());
fileResolver.setTemplateMode(TemplateMode.HTML);
fileResolver.setPrefix(StrUtil.addSuffixIfNot(config.getPath(), "/"));
resolver = fileResolver;
break;
case WEB_ROOT:
final FileTemplateResolver webRootResolver = new FileTemplateResolver();
webRootResolver.setCharacterEncoding(config.getCharsetStr());
webRootResolver.setTemplateMode(TemplateMode.HTML);
webRootResolver.setPrefix(StrUtil.addSuffixIfNot(FileUtil.getAbsolutePath(FileUtil.file(FileUtil.getWebRoot(), config.getPath())), "/"));
resolver = webRootResolver;
break;
case STRING:
resolver = new StringTemplateResolver();
break;
default:
resolver = new DefaultTemplateResolver();
break;
}
final org.thymeleaf.TemplateEngine engine = new org.thymeleaf.TemplateEngine();
engine.setTemplateResolver(resolver);
return engine;
}
use of cn.hutool.extra.template.TemplateEngine in project hutool by looly.
the class TemplateFactory method doCreate.
/**
* 根据用户引入的模板引擎jar,自动创建对应的模板引擎对象<br>
* 推荐创建的引擎单例使用,此方法每次调用会返回新的引擎
*
* @param config 模板配置,包括编码、模板文件path等信息
* @return {@link TemplateEngine}
*/
private static TemplateEngine doCreate(TemplateConfig config) {
final Class<? extends TemplateEngine> customEngineClass = config.getCustomEngine();
final TemplateEngine engine;
if (null != customEngineClass) {
engine = ReflectUtil.newInstance(customEngineClass);
} else {
engine = ServiceLoaderUtil.loadFirstAvailable(TemplateEngine.class);
}
if (null != engine) {
return engine.init(config);
}
throw new TemplateException("No template found ! Please add one of template jar to your project !");
}
Aggregations