Search in sources :

Example 16 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project adeptj-modules by AdeptJ.

the class ThymeleafViewEngine method activate.

@Activate
protected void activate(ViewEngineConfig config, BundleContext bundleContext) {
    TemplateEngine engine = new TemplateEngine();
    BundleTemplateResolver templateResolver = new BundleTemplateResolver(bundleContext.getBundle());
    templateResolver.setPrefix("views/");
    templateResolver.setSuffix(".html");
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setTemplateMode(TemplateMode.HTML);
    // default is cacheable
    templateResolver.setCacheable(false);
    templateResolver.setOrder(1);
    // RK: This is performance intensive OP, need to look into code for a
    // better alternative
    templateResolver.setCheckExistence(true);
    engine.addTemplateResolver(templateResolver);
}
Also used : TemplateEngine(org.thymeleaf.TemplateEngine) Activate(org.osgi.service.component.annotations.Activate)

Example 17 with TemplateEngine

use of org.thymeleaf.TemplateEngine 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 18 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project contribution by checkstyle.

the class TemplateProcessor method generateWithThymeleaf.

/**
 * Generates output file with release notes using Thymeleaf.
 * @param variables the map which represents template variables.
 * @param outputFile output file.
 * @param templateFilename template name.
 * @throws IOException if I/O error occurs.
 */
public static void generateWithThymeleaf(Map<String, Object> variables, String outputFile, String templateFilename) throws IOException {
    final TemplateEngine engine = new TemplateEngine();
    final AbstractConfigurableTemplateResolver resolver = new ClassLoaderTemplateResolver();
    resolver.setPrefix(TEMPLATE_FOLDER_PATH);
    engine.setTemplateResolver(resolver);
    final IContext ctx = new Context(Locale.US, variables);
    try (Writer fileWriter = new OutputStreamWriter(new FileOutputStream(outputFile), StandardCharsets.UTF_8)) {
        engine.process(templateFilename, ctx, fileWriter);
    }
}
Also used : IContext(org.thymeleaf.context.IContext) Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) IContext(org.thymeleaf.context.IContext) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) AbstractConfigurableTemplateResolver(org.thymeleaf.templateresolver.AbstractConfigurableTemplateResolver)

Example 19 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project contribution by checkstyle.

the class SiteGenerator method getTemplateEngine.

/**
 * Creates thymeleaf template engine.
 *
 * @return template engine.
 */
private static TemplateEngine getTemplateEngine() {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setTemplateMode("HTML");
    templateResolver.setPrefix("/");
    templateResolver.setSuffix(".template");
    final TemplateEngine tplEngine = new TemplateEngine();
    tplEngine.setTemplateResolver(templateResolver);
    return tplEngine;
}
Also used : ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TemplateEngine(org.thymeleaf.TemplateEngine)

Example 20 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project stdlib by petergeneric.

the class GuiceCoreTemplater method getOrCreateTemplater.

/**
 * Retrieve or build a Thymeleaf templater
 *
 * @return
 */
private ThymeleafTemplater getOrCreateTemplater() {
    ThymeleafTemplater templater = this.templater.get();
    // Lazy-create a ThymeleafTemplater
    if (templater == null) {
        final TemplateEngine engine = getOrCreateEngine();
        templater = new ThymeleafTemplater(engine, configuration, metrics, userProvider);
        templater.set("coreRestPrefix", coreRestPrefix);
        templater.set("coreRestEndpoint", coreRestEndpoint.toString());
        templater.set("coreServices", services);
        templater.set("currentUser", new ThymeleafCurrentUserUtils(userProvider));
        this.templater = new WeakReference<>(templater);
    }
    return templater;
}
Also used : TemplateEngine(org.thymeleaf.TemplateEngine)

Aggregations

TemplateEngine (org.thymeleaf.TemplateEngine)49 Context (org.thymeleaf.context.Context)28 Test (org.junit.Test)17 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)13 MockServletContext (org.springframework.mock.web.MockServletContext)11 RequestContext (org.springframework.web.servlet.support.RequestContext)10 Test (org.junit.jupiter.api.Test)7 WebContext (org.thymeleaf.context.WebContext)7 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)6 SpringTemplateEngine (org.thymeleaf.spring6.SpringTemplateEngine)6 ServletContext (jakarta.servlet.ServletContext)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)5 StringTemplateResolver (org.thymeleaf.templateresolver.StringTemplateResolver)5 ServletContextTemplateResolver (org.thymeleaf.templateresolver.ServletContextTemplateResolver)4 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)3 TemplateSpec (org.thymeleaf.TemplateSpec)3 Provides (com.google.inject.Provides)2 Singleton (com.google.inject.Singleton)2