Search in sources :

Example 26 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project spring-boot by spring-projects.

the class ThymeleafAutoConfigurationTests method renderNonWebAppTemplate.

@Test
public void renderNonWebAppTemplate() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    assertThat(context.getBeanNamesForType(ViewResolver.class).length).isEqualTo(0);
    try {
        TemplateEngine engine = context.getBean(TemplateEngine.class);
        Context attrs = new Context(Locale.UK, Collections.singletonMap("greeting", "Hello World"));
        String result = engine.process("message", attrs);
        assertThat(result).contains("Hello World");
    } finally {
        context.close();
    }
}
Also used : RequestContext(org.springframework.web.servlet.support.RequestContext) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Matchers.containsString(org.hamcrest.Matchers.containsString) ViewResolver(org.springframework.web.servlet.ViewResolver) ThymeleafViewResolver(org.thymeleaf.spring5.view.ThymeleafViewResolver) Test(org.junit.Test)

Example 27 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project sling by apache.

the class ThymeleafScriptEngineFactory method setupTemplateEngine.

private void setupTemplateEngine() {
    logger.info("setting up new template engine");
    templateEngine = null;
    if (configuration == null) {
        logger.info("configuration is null, not setting up new template engine");
        return;
    }
    if (!configuration.useStandardEngineContextFactory() && engineContextFactory == null) {
        logger.info("no engine context factory available, not setting up new template engine");
        return;
    }
    // setup template engine
    final TemplateEngine templateEngine = new TemplateEngine();
    // Template Resolvers
    if (this.templateResolvers != null) {
        final Set<ITemplateResolver> templateResolvers = new HashSet<>(this.templateResolvers);
        templateEngine.setTemplateResolvers(templateResolvers);
    }
    // Message Resolvers
    if (this.messageResolvers != null) {
        final Set<IMessageResolver> messageResolvers = new HashSet<>(this.messageResolvers);
        templateEngine.setMessageResolvers(messageResolvers);
    }
    if (configuration.useStandardMessageResolver()) {
        final IMessageResolver standardMessageResolver = new StandardMessageResolver();
        templateEngine.addMessageResolver(standardMessageResolver);
    }
    // Link Builders
    if (this.linkBuilders != null) {
        final Set<ILinkBuilder> linkBuilders = new HashSet<>(this.linkBuilders);
        templateEngine.setLinkBuilders(linkBuilders);
    }
    if (configuration.useStandardLinkBuilder()) {
        final ILinkBuilder standardLinkBuilder = new StandardLinkBuilder();
        templateEngine.addLinkBuilder(standardLinkBuilder);
    }
    // Dialects
    if (this.dialects != null) {
        final Set<IDialect> dialects = new HashSet<>(this.dialects);
        templateEngine.setDialects(dialects);
    }
    if (configuration.useStandardDialect()) {
        final IDialect standardDialect = new StandardDialect();
        templateEngine.addDialect(standardDialect);
    }
    // Decoupled Template Logic Resolver
    if (!configuration.useStandardDecoupledTemplateLogicResolver()) {
        templateEngine.setDecoupledTemplateLogicResolver(decoupledTemplateLogicResolver);
    }
    // Cache Manager
    if (!configuration.useStandardCacheManager()) {
        templateEngine.setCacheManager(cacheManager);
    }
    // Engine Context Factory
    if (!configuration.useStandardEngineContextFactory()) {
        templateEngine.setEngineContextFactory(engineContextFactory);
    }
    //
    this.templateEngine = templateEngine;
}
Also used : StandardLinkBuilder(org.thymeleaf.linkbuilder.StandardLinkBuilder) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver) IDialect(org.thymeleaf.dialect.IDialect) StandardMessageResolver(org.thymeleaf.messageresolver.StandardMessageResolver) ITemplateEngine(org.thymeleaf.ITemplateEngine) TemplateEngine(org.thymeleaf.TemplateEngine) IMessageResolver(org.thymeleaf.messageresolver.IMessageResolver) ILinkBuilder(org.thymeleaf.linkbuilder.ILinkBuilder) StandardDialect(org.thymeleaf.standard.StandardDialect) HashSet(java.util.HashSet)

Example 28 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project thymeleaf-tests by thymeleaf.

the class ParsingDecoupled01Test method testParsingDecoupled.

private static void testParsingDecoupled(final String decoupledTemplate, final TemplateMode templateMode, final String expectedResult) throws Exception {
    final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    templateResolver.setPrefix("templateparser/markup/");
    templateResolver.setSuffix(templateMode == TemplateMode.HTML ? ".html" : ".xml");
    templateResolver.setTemplateMode(templateMode);
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    // We only to this in order to initialize the engine
    templateEngine.process("parsingdecoupled", new Context());
    final IEngineConfiguration configuration = templateEngine.getConfiguration();
    final TemplateResolution templateResolution = templateResolver.resolveTemplate(configuration, null, decoupledTemplate, null);
    final ITemplateResource templateResource = templateResolution.getTemplateResource();
    final DecoupledTemplateLogic decoupledTemplateLogic = DecoupledTemplateLogicUtils.computeDecoupledTemplateLogic(configuration, null, decoupledTemplate, null, templateResource, templateMode, (templateMode == TemplateMode.HTML ? htmlParser : xmlParser));
    Assert.assertEquals(expectedResult, decoupledTemplateLogic.toString());
}
Also used : Context(org.thymeleaf.context.Context) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TemplateEngine(org.thymeleaf.TemplateEngine) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) DecoupledTemplateLogic(org.thymeleaf.templateparser.markup.decoupled.DecoupledTemplateLogic) TemplateResolution(org.thymeleaf.templateresolver.TemplateResolution) ITemplateResource(org.thymeleaf.templateresource.ITemplateResource)

Example 29 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project thymeleaf-tests by thymeleaf.

the class TemplateResolverAttributesTest method testTemplateResolutionAttributes01.

@Test
public void testTemplateResolutionAttributes01() throws Exception {
    final AttributeTesterTemplateResolver templateResolver = new AttributeTesterTemplateResolver(EXPECTED_ATTRIBUTES1);
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    final TemplateSpec templateSpec = new TemplateSpec("temp", EXPECTED_ATTRIBUTES1);
    final String output = templateEngine.process(templateSpec, new Context());
    Assert.assertTrue(templateResolver.tempCalled);
    Assert.assertTrue(templateResolver.fragCalled);
    Assert.assertEquals(EXPECTED_OUTPUT, output);
}
Also used : Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) TemplateSpec(org.thymeleaf.TemplateSpec) Test(org.junit.Test)

Example 30 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project thymeleaf-tests by thymeleaf.

the class TemplateResolverAttributesTest method testTemplateResolutionAttributes03.

@Test
public void testTemplateResolutionAttributes03() throws Exception {
    final AttributeTesterTemplateResolver templateResolver = new AttributeTesterTemplateResolver(EXPECTED_ATTRIBUTES3);
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    final TemplateSpec templateSpec = new TemplateSpec("temp", EXPECTED_ATTRIBUTES3);
    final String output = templateEngine.process(templateSpec, new Context());
    Assert.assertTrue(templateResolver.tempCalled);
    Assert.assertTrue(templateResolver.fragCalled);
    Assert.assertEquals(EXPECTED_OUTPUT, output);
}
Also used : Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) TemplateSpec(org.thymeleaf.TemplateSpec) Test(org.junit.Test)

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