Search in sources :

Example 1 with TemplateEngine

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;
}
Also used : TemplateEngine(cn.hutool.extra.template.TemplateEngine)

Example 2 with TemplateEngine

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;
}
Also used : ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TemplateEngine(cn.hutool.extra.template.TemplateEngine) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) FileTemplateResolver(org.thymeleaf.templateresolver.FileTemplateResolver) TemplateConfig(cn.hutool.extra.template.TemplateConfig) DefaultTemplateResolver(org.thymeleaf.templateresolver.DefaultTemplateResolver) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver)

Example 3 with TemplateEngine

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 !");
}
Also used : TemplateEngine(cn.hutool.extra.template.TemplateEngine) TemplateException(cn.hutool.extra.template.TemplateException)

Aggregations

TemplateEngine (cn.hutool.extra.template.TemplateEngine)3 TemplateConfig (cn.hutool.extra.template.TemplateConfig)1 TemplateException (cn.hutool.extra.template.TemplateException)1 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)1 DefaultTemplateResolver (org.thymeleaf.templateresolver.DefaultTemplateResolver)1 FileTemplateResolver (org.thymeleaf.templateresolver.FileTemplateResolver)1 ITemplateResolver (org.thymeleaf.templateresolver.ITemplateResolver)1 StringTemplateResolver (org.thymeleaf.templateresolver.StringTemplateResolver)1