Search in sources :

Example 1 with TemplateEngine

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

the class ThymeleafAutoConfigurationTests method useJava8TimeDialect.

@Test
public void useJava8TimeDialect() throws Exception {
    this.context.register(ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    TemplateEngine engine = this.context.getBean(TemplateEngine.class);
    Context attrs = new Context(Locale.UK);
    String result = engine.process("java8time-dialect", attrs);
    assertThat(result).isEqualTo("<html><body>2015-11-24</body></html>");
}
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) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 2 with TemplateEngine

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

the class ThymeleafAutoConfigurationTests method createFromConfigClass.

@Test
public void createFromConfigClass() throws Exception {
    EnvironmentTestUtils.addEnvironment(this.context, "spring.thymeleaf.mode:XHTML", "spring.thymeleaf.suffix:");
    this.context.register(ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    TemplateEngine engine = this.context.getBean(TemplateEngine.class);
    Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
    String result = engine.process("template.txt", attrs);
    assertThat(result).isEqualTo("<html>bar</html>");
}
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) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 3 with TemplateEngine

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

the class ThymeleafAutoConfigurationTests method useDataDialect.

@Test
public void useDataDialect() throws Exception {
    this.context.register(ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    TemplateEngine engine = this.context.getBean(TemplateEngine.class);
    Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
    String result = engine.process("data-dialect", attrs);
    assertThat(result).isEqualTo("<html><body data-foo=\"bar\"></body></html>");
}
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) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 4 with TemplateEngine

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

the class ThymeleafAutoConfigurationTests method renderTemplate.

@Test
public void renderTemplate() throws Exception {
    this.context.register(ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    TemplateEngine engine = this.context.getBean(TemplateEngine.class);
    Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
    String result = engine.process("home", attrs);
    assertThat(result).isEqualTo("<html><body>bar</body></html>");
}
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) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 5 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project ratpack by ratpack.

the class ThymeleafModule method provideTemplateEngine.

@Provides
@Singleton
TemplateEngine provideTemplateEngine(ITemplateResolver templateResolver, ICacheManager cacheManager, Set<IDialect> dialects) {
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    templateEngine.setCacheManager(cacheManager);
    dialects.stream().forEach(templateEngine::addDialect);
    return templateEngine;
}
Also used : TemplateEngine(org.thymeleaf.TemplateEngine) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Aggregations

TemplateEngine (org.thymeleaf.TemplateEngine)50 Context (org.thymeleaf.context.Context)30 Test (org.junit.Test)17 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)14 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.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 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)4 ServletContextTemplateResolver (org.thymeleaf.templateresolver.ServletContextTemplateResolver)4 StringTemplateResolver (org.thymeleaf.templateresolver.StringTemplateResolver)4 TemplateSpec (org.thymeleaf.TemplateSpec)3 IContext (org.thymeleaf.context.IContext)3 Provides (com.google.inject.Provides)2 Singleton (com.google.inject.Singleton)2