Search in sources :

Example 46 with TemplateEngine

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

the class ContextSequenceTest method testContextSequenceNoSpring.

public void testContextSequenceNoSpring() throws Exception {
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(new ClassLoaderTemplateResolver());
    final Context ctx1 = new Context();
    ctx1.setVariable("myObject", new MyObjectClass("one"));
    final Context ctx2 = new Context();
    ctx2.setVariable("myObject", new MyObjectClass("two"));
    final String result1 = ResourceUtils.normalize(templateEngine.process("context/contextSequence.html", ctx1));
    final String result2 = ResourceUtils.normalize(templateEngine.process("context/contextSequence.html", ctx2));
    final String expected1 = ResourceUtils.read(ClassLoaderUtils.getClassLoader(ContextSequenceTest.class).getResourceAsStream("context/contextSequence-result1.html"), "UTF-8", true);
    final String expected2 = ResourceUtils.read(ClassLoaderUtils.getClassLoader(ContextSequenceTest.class).getResourceAsStream("context/contextSequence-result2.html"), "UTF-8", true);
    Assert.assertEquals(expected1, result1);
    Assert.assertEquals(expected2, result2);
}
Also used : TemplateEngine(org.thymeleaf.TemplateEngine) SpringTemplateEngine(org.thymeleaf.spring4.SpringTemplateEngine) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)

Example 47 with TemplateEngine

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

the class ThymeleafServletAutoConfigurationTests method createFromConfigClass.

@Test
void createFromConfigClass() {
    this.contextRunner.withPropertyValues("spring.thymeleaf.mode:HTML", "spring.thymeleaf.suffix:").run((context) -> {
        assertThat(context).hasSingleBean(TemplateEngine.class);
        TemplateEngine engine = context.getBean(TemplateEngine.class);
        Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
        String result = engine.process("template.html", attrs).trim();
        assertThat(result).isEqualTo("<html>bar</html>");
    });
}
Also used : RequestContext(org.springframework.web.servlet.support.RequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) WebContext(org.thymeleaf.context.WebContext) ServletContext(jakarta.servlet.ServletContext) Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) SpringTemplateEngine(org.thymeleaf.spring6.SpringTemplateEngine) Test(org.junit.jupiter.api.Test)

Example 48 with TemplateEngine

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

the class ThymeleafServletAutoConfigurationTests method useDataDialect.

@Test
void useDataDialect() {
    this.contextRunner.run((context) -> {
        TemplateEngine engine = context.getBean(TemplateEngine.class);
        Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
        String result = engine.process("data-dialect", attrs).trim();
        assertThat(result).isEqualTo("<html><body data-foo=\"bar\"></body></html>");
    });
}
Also used : RequestContext(org.springframework.web.servlet.support.RequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) WebContext(org.thymeleaf.context.WebContext) ServletContext(jakarta.servlet.ServletContext) Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) SpringTemplateEngine(org.thymeleaf.spring6.SpringTemplateEngine) Test(org.junit.jupiter.api.Test)

Example 49 with TemplateEngine

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

the class ThymeleafServletAutoConfigurationTests method renderTemplate.

@Test
void renderTemplate() {
    this.contextRunner.run((context) -> {
        TemplateEngine engine = context.getBean(TemplateEngine.class);
        Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
        String result = engine.process("home", attrs).trim();
        assertThat(result).isEqualTo("<html><body>bar</body></html>");
    });
}
Also used : RequestContext(org.springframework.web.servlet.support.RequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) WebContext(org.thymeleaf.context.WebContext) ServletContext(jakarta.servlet.ServletContext) Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) SpringTemplateEngine(org.thymeleaf.spring6.SpringTemplateEngine) Test(org.junit.jupiter.api.Test)

Example 50 with TemplateEngine

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

the class SiteGenerator method generate.

/**
 * Generates site report using thymeleaf template engine.
 *
 * @param diffReport
 *        container with parsed data.
 * @param diffConfiguration
 *        merged configurations from both reports.
 * @param paths
 *        CLI paths.
 * @throws IOException
 *         on failure to write site to disc.
 */
public static void generate(DiffReport diffReport, MergedConfigurationModule diffConfiguration, CliPaths paths) throws IOException {
    // setup thymeleaf engine
    final TemplateEngine tplEngine = getTemplateEngine();
    // setup xreference generator
    final XrefGenerator xrefGenerator = new XrefGenerator(paths.getRefFilesPath(), paths.getOutputPath().resolve(Main.XREF_FILEPATH), paths.getOutputPath());
    // html generation
    final Path sitepath = paths.getOutputPath().resolve(SITEPATH);
    final FileWriter writer = new FileWriter(sitepath.toString());
    try {
        // write statistics
        generateHeader(tplEngine, writer, diffReport.getStatistics(), diffConfiguration);
        // write parsed content
        generateBody(tplEngine, writer, diffReport, paths, xrefGenerator);
        // write html footer
        tplEngine.process("footer", new Context(), writer);
    } finally {
        writer.close();
    }
}
Also used : Path(java.nio.file.Path) Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) FileWriter(java.io.FileWriter)

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