Search in sources :

Example 6 with DefaultTemplateResolver

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

the class TemplateEngineTest method testDefaultTemplateResolver03.

@Test
public void testDefaultTemplateResolver03() {
    final TemplateEngine templateEngine = new TemplateEngine();
    final Context context = new Context();
    context.setLocale(Locale.ENGLISH);
    context.setVariable("one", "this value");
    final ClassLoaderTemplateResolver classLoaderTemplateResolver = new ClassLoaderTemplateResolver();
    classLoaderTemplateResolver.setCheckExistence(true);
    templateEngine.addTemplateResolver(classLoaderTemplateResolver);
    final DefaultTemplateResolver defaultTemplateResolver = new DefaultTemplateResolver();
    defaultTemplateResolver.setTemplate("<p th:text=\"${one}\">something</p>");
    templateEngine.addTemplateResolver(defaultTemplateResolver);
    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.DefaultTemplateResolver", templateResolvers.get(1).getName());
    Assert.assertEquals("<p>this value</p>", templateEngine.process("nonexisting", context));
}
Also used : Context(org.thymeleaf.context.Context) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) ArrayList(java.util.ArrayList) DefaultTemplateResolver(org.thymeleaf.templateresolver.DefaultTemplateResolver) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)6 Context (org.thymeleaf.context.Context)6 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)6 DefaultTemplateResolver (org.thymeleaf.templateresolver.DefaultTemplateResolver)6 ArrayList (java.util.ArrayList)4 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)4 ITemplateResolver (org.thymeleaf.templateresolver.ITemplateResolver)4 StringTemplateResolver (org.thymeleaf.templateresolver.StringTemplateResolver)2