Search in sources :

Example 1 with TemplateConfig

use of cn.hutool.extra.template.TemplateConfig in project hutool by looly.

the class FreemarkerEngine method createCfg.

/**
 * 创建配置项
 *
 * @param config 模板配置
 * @return {@link Configuration }
 */
private static Configuration createCfg(TemplateConfig config) {
    if (null == config) {
        config = new TemplateConfig();
    }
    final Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
    cfg.setLocalizedLookup(false);
    cfg.setDefaultEncoding(config.getCharset().toString());
    switch(config.getResourceMode()) {
        case CLASSPATH:
            cfg.setTemplateLoader(new ClassTemplateLoader(ClassUtil.getClassLoader(), config.getPath()));
            break;
        case FILE:
            try {
                cfg.setTemplateLoader(new FileTemplateLoader(FileUtil.file(config.getPath())));
            } catch (IOException e) {
                throw new IORuntimeException(e);
            }
            break;
        case WEB_ROOT:
            try {
                cfg.setTemplateLoader(new FileTemplateLoader(FileUtil.file(FileUtil.getWebRoot(), config.getPath())));
            } catch (IOException e) {
                throw new IORuntimeException(e);
            }
            break;
        case STRING:
            cfg.setTemplateLoader(new SimpleStringTemplateLoader());
            break;
        default:
            break;
    }
    return cfg;
}
Also used : Configuration(freemarker.template.Configuration) IORuntimeException(cn.hutool.core.io.IORuntimeException) ClassTemplateLoader(freemarker.cache.ClassTemplateLoader) TemplateConfig(cn.hutool.extra.template.TemplateConfig) IOException(java.io.IOException) FileTemplateLoader(freemarker.cache.FileTemplateLoader)

Example 2 with TemplateConfig

use of cn.hutool.extra.template.TemplateConfig 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 TemplateConfig

use of cn.hutool.extra.template.TemplateConfig in project hutool by looly.

the class VelocityEngine method createEngine.

/**
 * 创建引擎
 *
 * @param config 模板配置
 * @return {@link org.apache.velocity.app.VelocityEngine}
 */
private static org.apache.velocity.app.VelocityEngine createEngine(TemplateConfig config) {
    if (null == config) {
        config = new TemplateConfig();
    }
    final org.apache.velocity.app.VelocityEngine ve = new org.apache.velocity.app.VelocityEngine();
    // 编码
    final String charsetStr = config.getCharset().toString();
    ve.setProperty(Velocity.INPUT_ENCODING, charsetStr);
    // ve.setProperty(Velocity.OUTPUT_ENCODING, charsetStr);
    // 使用缓存
    ve.setProperty(Velocity.FILE_RESOURCE_LOADER_CACHE, true);
    // loader
    switch(config.getResourceMode()) {
        case CLASSPATH:
            // 新版Velocity弃用
            // ve.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            ve.setProperty("resource.loader.file.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            break;
        case FILE:
            // path
            final String path = config.getPath();
            if (null != path) {
                ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
            }
            break;
        case WEB_ROOT:
            ve.setProperty(Velocity.RESOURCE_LOADER, "webapp");
            ve.setProperty("webapp.resource.loader.class", "org.apache.velocity.tools.view.servlet.WebappLoader");
            ve.setProperty("webapp.resource.loader.path", StrUtil.nullToDefault(config.getPath(), StrUtil.SLASH));
            break;
        case STRING:
            ve.setProperty(Velocity.RESOURCE_LOADER, "str");
            ve.setProperty("str.resource.loader.class", SimpleStringResourceLoader.class.getName());
            break;
        default:
            break;
    }
    ve.init();
    return ve;
}
Also used : TemplateConfig(cn.hutool.extra.template.TemplateConfig)

Example 4 with TemplateConfig

use of cn.hutool.extra.template.TemplateConfig in project hutool by looly.

the class RythmEngine method createEngine.

/**
 * 创建引擎
 *
 * @param config 模板配置
 * @return {@link org.rythmengine.RythmEngine}
 */
private static org.rythmengine.RythmEngine createEngine(TemplateConfig config) {
    if (null == config) {
        config = new TemplateConfig();
    }
    final Properties props = new Properties();
    final String path = config.getPath();
    if (null != path) {
        props.put("home.template", path);
    }
    return new org.rythmengine.RythmEngine(props);
}
Also used : TemplateConfig(cn.hutool.extra.template.TemplateConfig) Properties(java.util.Properties)

Aggregations

TemplateConfig (cn.hutool.extra.template.TemplateConfig)4 IORuntimeException (cn.hutool.core.io.IORuntimeException)1 TemplateEngine (cn.hutool.extra.template.TemplateEngine)1 ClassTemplateLoader (freemarker.cache.ClassTemplateLoader)1 FileTemplateLoader (freemarker.cache.FileTemplateLoader)1 Configuration (freemarker.template.Configuration)1 IOException (java.io.IOException)1 Properties (java.util.Properties)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