Search in sources :

Example 36 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 : SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) TemplateEngine(org.thymeleaf.TemplateEngine) ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)

Example 37 with TemplateEngine

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

the class DialectOrderingTest method combineDialects.

private static Set<IElementProcessor> combineDialects(final IDialect... dialects) {
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setDialect(dialects[0]);
    for (int i = 1; i < dialects.length; i++) {
        templateEngine.addDialect(dialects[i]);
    }
    return templateEngine.getConfiguration().getElementProcessors(TemplateMode.HTML);
}
Also used : TemplateEngine(org.thymeleaf.TemplateEngine)

Example 38 with TemplateEngine

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

the class DialectProcessWrappingTest method testDialectWrapping.

@Test
public void testDialectWrapping() throws Exception {
    final Dialect01 dialect01 = new Dialect01();
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setDialect(dialect01);
    final IEngineConfiguration config = templateEngine.getConfiguration();
    final List<IElementProcessor> elementProcessors = new ArrayList<IElementProcessor>(config.getElementProcessors(TemplateMode.HTML));
    final List<ICDATASectionProcessor> cdataSectionProcessors = new ArrayList<ICDATASectionProcessor>(config.getCDATASectionProcessors(TemplateMode.HTML));
    final List<ICommentProcessor> commentProcessors = new ArrayList<ICommentProcessor>(config.getCommentProcessors(TemplateMode.HTML));
    final List<IDocTypeProcessor> docTypeProcessors = new ArrayList<IDocTypeProcessor>(config.getDocTypeProcessors(TemplateMode.HTML));
    final List<IProcessingInstructionProcessor> processingInstructionProcessors = new ArrayList<IProcessingInstructionProcessor>(config.getProcessingInstructionProcessors(TemplateMode.HTML));
    final List<ITemplateBoundariesProcessor> templateBoundariesProcessors = new ArrayList<ITemplateBoundariesProcessor>(config.getTemplateBoundariesProcessors(TemplateMode.HTML));
    final List<ITextProcessor> textProcessors = new ArrayList<ITextProcessor>(config.getTextProcessors(TemplateMode.HTML));
    final List<IXMLDeclarationProcessor> xmlDeclarationProcessors = new ArrayList<IXMLDeclarationProcessor>(config.getXMLDeclarationProcessors(TemplateMode.HTML));
    Assert.assertEquals(2, elementProcessors.size());
    Assert.assertEquals(1, cdataSectionProcessors.size());
    Assert.assertEquals(1, commentProcessors.size());
    Assert.assertEquals(1, docTypeProcessors.size());
    Assert.assertEquals(1, processingInstructionProcessors.size());
    Assert.assertEquals(1, templateBoundariesProcessors.size());
    Assert.assertEquals(1, textProcessors.size());
    Assert.assertEquals(1, xmlDeclarationProcessors.size());
    // We will use the class names because the classes are package-protected
    Assert.assertEquals("org.thymeleaf.util.ProcessorConfigurationUtils$ElementModelProcessorWrapper", elementProcessors.get(0).getClass().getName());
    Assert.assertEquals("org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper", elementProcessors.get(1).getClass().getName());
    Assert.assertEquals("org.thymeleaf.util.ProcessorConfigurationUtils$CDATASectionProcessorWrapper", cdataSectionProcessors.get(0).getClass().getName());
    Assert.assertEquals("org.thymeleaf.util.ProcessorConfigurationUtils$CommentProcessorWrapper", commentProcessors.get(0).getClass().getName());
    Assert.assertEquals("org.thymeleaf.util.ProcessorConfigurationUtils$DocTypeProcessorWrapper", docTypeProcessors.get(0).getClass().getName());
    Assert.assertEquals("org.thymeleaf.util.ProcessorConfigurationUtils$ProcessingInstructionProcessorWrapper", processingInstructionProcessors.get(0).getClass().getName());
    Assert.assertEquals("org.thymeleaf.util.ProcessorConfigurationUtils$TemplateBoundariesProcessorWrapper", templateBoundariesProcessors.get(0).getClass().getName());
    Assert.assertEquals("org.thymeleaf.util.ProcessorConfigurationUtils$TextProcessorWrapper", textProcessors.get(0).getClass().getName());
    Assert.assertEquals("org.thymeleaf.util.ProcessorConfigurationUtils$XMLDeclarationProcessorWrapper", xmlDeclarationProcessors.get(0).getClass().getName());
    Assert.assertEquals(100, elementProcessors.get(0).getPrecedence());
    Assert.assertEquals(110, elementProcessors.get(1).getPrecedence());
    Assert.assertEquals(100, cdataSectionProcessors.get(0).getPrecedence());
    Assert.assertEquals(100, commentProcessors.get(0).getPrecedence());
    Assert.assertEquals(100, docTypeProcessors.get(0).getPrecedence());
    Assert.assertEquals(100, processingInstructionProcessors.get(0).getPrecedence());
    Assert.assertEquals(100, templateBoundariesProcessors.get(0).getPrecedence());
    Assert.assertEquals(100, textProcessors.get(0).getPrecedence());
    Assert.assertEquals(100, xmlDeclarationProcessors.get(0).getPrecedence());
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) ITextProcessor(org.thymeleaf.processor.text.ITextProcessor) IProcessingInstructionProcessor(org.thymeleaf.processor.processinginstruction.IProcessingInstructionProcessor) ArrayList(java.util.ArrayList) ITemplateBoundariesProcessor(org.thymeleaf.processor.templateboundaries.ITemplateBoundariesProcessor) Dialect01(org.thymeleaf.dialect.dialectwrapping.Dialect01) IDocTypeProcessor(org.thymeleaf.processor.doctype.IDocTypeProcessor) IElementProcessor(org.thymeleaf.processor.element.IElementProcessor) ICommentProcessor(org.thymeleaf.processor.comment.ICommentProcessor) TemplateEngine(org.thymeleaf.TemplateEngine) IXMLDeclarationProcessor(org.thymeleaf.processor.xmldeclaration.IXMLDeclarationProcessor) ICDATASectionProcessor(org.thymeleaf.processor.cdatasection.ICDATASectionProcessor) Test(org.junit.Test)

Example 39 with TemplateEngine

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

the class ExpressionTest method setUp.

@Override
protected void setUp() throws Exception {
    this.templateEngine = new TemplateEngine();
    this.templateEngine.setTemplateResolver(new TestTemplateResolver(TEMPLATE));
    this.contextES = new Context(LOCALE_ES);
    this.contextEN = new Context(LOCALE_EN);
    final Department deptAccounting = new Department(Integer.valueOf(1), "Accounting and Finance");
    final Department deptEngineering = new Department(Integer.valueOf(2), "Engineering and Consulting");
    final Department deptSales = new Department(Integer.valueOf(3), "Sales");
    final Department deptMarketing = new Department(Integer.valueOf(4), "Marketing");
    final List<String> logins = new ArrayList<String>();
    final Map<String, Object> objects = new HashMap<String, Object>();
    {
        final User user = new User("loceania", "Leslie Oceania", deptMarketing, 3, toCalendar(2004, 11, 23), 5.3, false, toArray("Event Organizer", "Marketing Worldwide Head", "Office Master"));
        objects.put(user.getLogin(), user);
        logins.add(user.getLogin());
    }
    {
        final User user = new User("meurope", "Markus Europe", deptEngineering, 5, toCalendar(2008, 1, 3), 8.0, true, null);
        objects.put(user.getLogin(), user);
        logins.add(user.getLogin());
    }
    {
        final User user = new User("jafrica", "Jacques Africa", deptSales, 3, toCalendar(2010, 9, 23), 4.3, false, toArray("Sales Manager", "Department Director"));
        objects.put(user.getLogin(), user);
        logins.add(user.getLogin());
    }
    {
        final User user = new User("pamerica", "Petronila America", deptAccounting, 1, toCalendar(2002, 4, 19), 9.2, false, null);
        objects.put(user.getLogin(), user);
        logins.add(user.getLogin());
    }
    objects.put("currentYear", Integer.valueOf(2011));
    objects.put("logins", logins);
    objects.put("loginsArray", logins.toArray(new String[logins.size()]));
    objects.put("size", "Size is 5");
    this.contextES.setVariables(objects);
    this.contextEN.setVariables(objects);
    final Properties props = new Properties();
    props.put("application.name", "Thymeleaf test");
    props.put("hello.message", "Hello, {0}!");
    props.put("today", "Today is {0,date,dd/MM/yyyy}, so hello {1}!");
    props.put("title.dept.Marketing", "The almighty Marketing department");
    props.put("title.user.meurope", "User {0} works for the {1} department");
    props.put("priority.basic", "3");
    props.put("company.yearfounded", "1976");
    props.put("dateForPath", "10/10/1976");
    props.put("sum", "1+1=2");
    this.templateEngine.setMessageResolver(new TestMessageResolver(props));
}
Also used : IContext(org.thymeleaf.context.IContext) Context(org.thymeleaf.context.Context) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Properties(java.util.Properties) TemplateEngine(org.thymeleaf.TemplateEngine)

Example 40 with TemplateEngine

use of org.thymeleaf.TemplateEngine in project java-docs-samples by GoogleCloudPlatform.

the class GaeInfoServlet method init.

@Override
public void init() {
    // Setup ThymeLeaf
    ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(this.getServletContext());
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    // TTL=20m
    templateResolver.setCacheTTLMs(Long.valueOf(1200000L));
    // Cache is set to true by default. Set to false if you want templates to
    // be automatically updated when modified.
    templateResolver.setCacheable(true);
    templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
}
Also used : TemplateEngine(org.thymeleaf.TemplateEngine) ServletContextTemplateResolver(org.thymeleaf.templateresolver.ServletContextTemplateResolver)

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