Search in sources :

Example 6 with FileTemplateResolver

use of org.thymeleaf.templateresolver.FileTemplateResolver in project thymeleaf-tests by thymeleaf.

the class TemplateEngineTest method testTemplateResolverConfiguration05.

@Test
public void testTemplateResolverConfiguration05() {
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(new ClassLoaderTemplateResolver());
    templateEngine.addTemplateResolver(new FileTemplateResolver());
    templateEngine.initialize();
    final List<ITemplateResolver> templateResolvers = new ArrayList<ITemplateResolver>(templateEngine.getTemplateResolvers());
    Assert.assertEquals(2, templateResolvers.size());
    Assert.assertEquals("org.thymeleaf.templateresolver.ClassLoaderTemplateResolver", templateResolvers.get(0).getName());
    Assert.assertEquals("org.thymeleaf.templateresolver.FileTemplateResolver", templateResolvers.get(1).getName());
}
Also used : SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) FileTemplateResolver(org.thymeleaf.templateresolver.FileTemplateResolver) ArrayList(java.util.ArrayList) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver) Test(org.junit.Test)

Example 7 with FileTemplateResolver

use of org.thymeleaf.templateresolver.FileTemplateResolver in project cas by apereo.

the class CasCoreViewsConfiguration method chainingTemplateViewResolver.

@Bean
public AbstractTemplateResolver chainingTemplateViewResolver() {
    final ChainingTemplateViewResolver chain = new ChainingTemplateViewResolver();
    casProperties.getView().getTemplatePrefixes().forEach(Unchecked.consumer(prefix -> {
        final String prefixPath = ResourceUtils.getFile(prefix).getCanonicalPath();
        final String viewPath = StringUtils.appendIfMissing(prefixPath, "/");
        final ThemeFileTemplateResolver theme = new ThemeFileTemplateResolver(casProperties);
        configureTemplateViewResolver(theme);
        theme.setPrefix(viewPath + "themes/%s/");
        chain.addResolver(theme);
        final FileTemplateResolver file = new FileTemplateResolver();
        configureTemplateViewResolver(file);
        file.setPrefix(viewPath);
        chain.addResolver(file);
    }));
    return chain;
}
Also used : CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) AbstractConfigurableTemplateResolver(org.thymeleaf.templateresolver.AbstractConfigurableTemplateResolver) Unchecked(org.jooq.lambda.Unchecked) AbstractTemplateResolver(org.thymeleaf.templateresolver.AbstractTemplateResolver) ThymeleafProperties(org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ResourceUtils(org.springframework.util.ResourceUtils) Configuration(org.springframework.context.annotation.Configuration) Slf4j(lombok.extern.slf4j.Slf4j) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) ThemeFileTemplateResolver(org.apereo.cas.web.view.ThemeFileTemplateResolver) Bean(org.springframework.context.annotation.Bean) FileTemplateResolver(org.thymeleaf.templateresolver.FileTemplateResolver) ChainingTemplateViewResolver(org.apereo.cas.web.view.ChainingTemplateViewResolver) ChainingTemplateViewResolver(org.apereo.cas.web.view.ChainingTemplateViewResolver) ThemeFileTemplateResolver(org.apereo.cas.web.view.ThemeFileTemplateResolver) FileTemplateResolver(org.thymeleaf.templateresolver.FileTemplateResolver) ThemeFileTemplateResolver(org.apereo.cas.web.view.ThemeFileTemplateResolver) Bean(org.springframework.context.annotation.Bean)

Example 8 with FileTemplateResolver

use of org.thymeleaf.templateresolver.FileTemplateResolver in project docx4j-template by vindell.

the class WordprocessingMLThymeleafTemplate method getInternalEngine.

protected TemplateEngine getInternalEngine() throws IOException {
    // 初始化模板解析器
    AbstractConfigurableTemplateResolver templateResolver = getTemplateResolver();
    if (getTemplateResolver() == null) {
        String resolver = Docx4jProperties.getProperty("docx4j.thymeleaf.templateResolver", "org.thymeleaf.templateresolver.FileTemplateResolver");
        if ("org.thymeleaf.templateresolver.FileTemplateResolver".equalsIgnoreCase(resolver)) {
            templateResolver = new FileTemplateResolver();
        } else if ("org.thymeleaf.templateresolver.ClassLoaderTemplateResolver".equalsIgnoreCase(resolver)) {
            templateResolver = new ClassLoaderTemplateResolver();
        } else if ("org.thymeleaf.templateresolver.UrlTemplateResolver".equalsIgnoreCase(resolver)) {
            templateResolver = new UrlTemplateResolver();
        } else {
            templateResolver = new FileTemplateResolver();
        }
    }
    templateResolver.setCacheable(Docx4jProperties.getProperty("docx4j.thymeleaf.cacheable", true));
    templateResolver.setCacheablePatterns(ArrayUtils.asSet(StringUtils.tokenizeToStringArray(Docx4jProperties.getProperty("docx4j.thymeleaf.cacheablePatterns", ""))));
    String cacheTTLMs = Docx4jProperties.getProperty("docx4j.thymeleaf.cacheTTLMs");
    templateResolver.setCacheTTLMs(cacheTTLMs == null ? null : Long.valueOf(cacheTTLMs));
    templateResolver.setCharacterEncoding(Docx4jProperties.getProperty("docx4j.thymeleaf.charset", "UTF-8"));
    templateResolver.setCheckExistence(Docx4jProperties.getProperty("docx4j.thymeleaf.checkExistence", false));
    templateResolver.setCSSTemplateModePatterns(ArrayUtils.asSet(StringUtils.tokenizeToStringArray(Docx4jProperties.getProperty("docx4j.thymeleaf.newCSSTemplateModePatterns", ""))));
    templateResolver.setHtmlTemplateModePatterns(ArrayUtils.asSet(StringUtils.tokenizeToStringArray(Docx4jProperties.getProperty("docx4j.thymeleaf.newHtmlTemplateModePatterns", ""))));
    templateResolver.setJavaScriptTemplateModePatterns(ArrayUtils.asSet(StringUtils.tokenizeToStringArray(Docx4jProperties.getProperty("docx4j.thymeleaf.newJavaScriptTemplateModePatterns", ""))));
    templateResolver.setName(Docx4jProperties.getProperty("docx4j.thymeleaf.name", templateResolver.getClass().getName()));
    templateResolver.setNonCacheablePatterns(ArrayUtils.asSet(StringUtils.tokenizeToStringArray(Docx4jProperties.getProperty("docx4j.thymeleaf.nonCacheablePatterns", ""))));
    templateResolver.setOrder(Integer.valueOf(Docx4jProperties.getProperty("docx4j.thymeleaf.order", "1")));
    templateResolver.setPrefix(Docx4jProperties.getProperty("docx4j.thymeleaf.prefix"));
    templateResolver.setRawTemplateModePatterns(ArrayUtils.asSet(StringUtils.tokenizeToStringArray(Docx4jProperties.getProperty("docx4j.thymeleaf.newRawTemplateModePatterns", ""))));
    templateResolver.setResolvablePatterns(ArrayUtils.asSet(StringUtils.tokenizeToStringArray(Docx4jProperties.getProperty("docx4j.thymeleaf.resolvablePatterns", ""))));
    templateResolver.setSuffix(Docx4jProperties.getProperty("docx4j.thymeleaf.suffix", ".tpl"));
    // templateResolver.setTemplateAliases(templateAliases);
    templateResolver.setTemplateMode(Docx4jProperties.getProperty("docx4j.thymeleaf.templateMode", "XHTML"));
    templateResolver.setTextTemplateModePatterns(ArrayUtils.asSet(StringUtils.tokenizeToStringArray(Docx4jProperties.getProperty("docx4j.thymeleaf.newTextTemplateModePatterns", ""))));
    templateResolver.setUseDecoupledLogic(Docx4jProperties.getProperty("docx4j.thymeleaf.useDecoupledLogic", false));
    templateResolver.setXmlTemplateModePatterns(ArrayUtils.asSet(StringUtils.tokenizeToStringArray(Docx4jProperties.getProperty("docx4j.thymeleaf.newXmlTemplateModePatterns", ""))));
    // 初始化引擎对象
    TemplateEngine engine = new TemplateEngine();
    engine.setTemplateResolver(templateResolver);
    // 调用getConfiguration初始化引擎
    engine.getConfiguration();
    return engine;
}
Also used : ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TemplateEngine(org.thymeleaf.TemplateEngine) FileTemplateResolver(org.thymeleaf.templateresolver.FileTemplateResolver) UrlTemplateResolver(org.thymeleaf.templateresolver.UrlTemplateResolver) AbstractConfigurableTemplateResolver(org.thymeleaf.templateresolver.AbstractConfigurableTemplateResolver)

Example 9 with FileTemplateResolver

use of org.thymeleaf.templateresolver.FileTemplateResolver in project thymeleaf-tests by thymeleaf.

the class TemplateEngineTest method testTemplateResolverConfiguration10.

@Test
public void testTemplateResolverConfiguration10() {
    final TemplateEngine templateEngine = new SpringTemplateEngine();
    final Set<ITemplateResolver> resolvers = new LinkedHashSet<ITemplateResolver>();
    resolvers.add(new ClassLoaderTemplateResolver());
    resolvers.add(new FileTemplateResolver());
    templateEngine.setTemplateResolvers(resolvers);
    templateEngine.initialize();
    final List<ITemplateResolver> templateResolvers = new ArrayList<ITemplateResolver>(templateEngine.getTemplateResolvers());
    Assert.assertEquals(2, templateResolvers.size());
    Assert.assertEquals("org.thymeleaf.templateresolver.ClassLoaderTemplateResolver", templateResolvers.get(0).getName());
    Assert.assertEquals("org.thymeleaf.templateresolver.FileTemplateResolver", templateResolvers.get(1).getName());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) FileTemplateResolver(org.thymeleaf.templateresolver.FileTemplateResolver) ArrayList(java.util.ArrayList) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver) Test(org.junit.Test)

Aggregations

FileTemplateResolver (org.thymeleaf.templateresolver.FileTemplateResolver)9 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)8 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)7 ITemplateResolver (org.thymeleaf.templateresolver.ITemplateResolver)7 LinkedHashSet (java.util.LinkedHashSet)2 AbstractConfigurableTemplateResolver (org.thymeleaf.templateresolver.AbstractConfigurableTemplateResolver)2 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.apache.commons.lang3.StringUtils)1 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)1 ChainingTemplateViewResolver (org.apereo.cas.web.view.ChainingTemplateViewResolver)1 ThemeFileTemplateResolver (org.apereo.cas.web.view.ThemeFileTemplateResolver)1 Unchecked (org.jooq.lambda.Unchecked)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 ThymeleafProperties (org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties)1 EnableConfigurationProperties (org.springframework.boot.context.properties.EnableConfigurationProperties)1 Bean (org.springframework.context.annotation.Bean)1 Configuration (org.springframework.context.annotation.Configuration)1 ResourceUtils (org.springframework.util.ResourceUtils)1