Search in sources :

Example 6 with StringTemplateResolver

use of org.thymeleaf.templateresolver.StringTemplateResolver in project thymeleaf by thymeleaf.

the class TemplateEngine method initialize.

/**
 * <p>
 *   Internal method that initializes the Template Engine instance. This method
 *   is called before the first execution of {@link TemplateEngine#process(String, IContext)}
 *   or {@link TemplateEngine#processThrottled(String, IContext)}
 *   in order to create all the structures required for a quick execution of
 *   templates.
 * </p>
 * <p>
 *   THIS METHOD IS INTERNAL AND SHOULD <b>NEVER</b> BE CALLED DIRECTLY.
 * </p>
 * <p>
 *   If a subclass of <tt>TemplateEngine</tt> needs additional steps for
 *   initialization, the {@link #initializeSpecific()} method should
 *   be overridden.
 * </p>
 */
final void initialize() {
    if (!this.initialized) {
        synchronized (this) {
            if (!this.initialized) {
                logger.debug("[THYMELEAF] INITIALIZING TEMPLATE ENGINE");
                // First of all, give subclasses the opportunity to modify the base configuration
                initializeSpecific();
                // We need at least one template resolver at this point - we set the StringTemplateResolver as default
                if (this.templateResolvers.isEmpty()) {
                    this.templateResolvers.add(new StringTemplateResolver());
                }
                // Build the EngineConfiguration object
                this.configuration = new EngineConfiguration(this.templateResolvers, this.messageResolvers, this.linkBuilders, this.dialectConfigurations, this.cacheManager, this.engineContextFactory, this.decoupledTemplateLogicResolver);
                ((EngineConfiguration) this.configuration).initialize();
                this.initialized = true;
                // Log configuration details
                ConfigurationPrinterHelper.printConfiguration(this.configuration);
                logger.debug("[THYMELEAF] TEMPLATE ENGINE INITIALIZED");
            }
        }
    }
}
Also used : StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver)

Example 7 with StringTemplateResolver

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

the class ScriptInlineTest method testInlineResult.

private static void testInlineResult(final String script, final String expectedResult, final Map<String, Object> variables) {
    final String completeScript = "<script th:inline=\"javascript\">\n/*<![CDATA[ */\n" + script + "\n/* ]]> */\n</script>";
    final ITemplateResolver templateResolver = new StringTemplateResolver();
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(templateResolver);
    final Context ctx = new Context();
    ctx.setVariables(variables);
    final StringWriter stringWriter = new StringWriter();
    templateEngine.process(completeScript, ctx, stringWriter);
    final String result = stringWriter.toString();
    final String extractedResult = result.substring(0, result.indexOf("\n/* ]]> */\n</script>")).substring(24);
    Assert.assertEquals(expectedResult, extractedResult);
}
Also used : Context(org.thymeleaf.context.Context) TemplateEngine(org.thymeleaf.TemplateEngine) StringWriter(java.io.StringWriter) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver)

Example 8 with StringTemplateResolver

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

the class LinkBuilderTest method testLinkBuilderSpring01.

@Test
public void testLinkBuilderSpring01() throws Exception {
    final Context ctx = new Context();
    ctx.setVariable("one", "This is one");
    final TemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(new StringTemplateResolver());
    templateEngine.setLinkBuilder(new TestLinkBuilder());
    final String result = templateEngine.process("<a th:href='@{/something}'>", ctx);
    final String expected = "<a href='[/fromthebuilder/something]'>";
    Assert.assertEquals(expected, result);
}
Also used : Context(org.thymeleaf.context.Context) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) TemplateEngine(org.thymeleaf.TemplateEngine) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) Test(org.junit.Test)

Example 9 with StringTemplateResolver

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

the class LinkBuilderTest method testLinkBuilderWithECFactory01.

@Test
public void testLinkBuilderWithECFactory01() throws Exception {
    final Context ctx = new Context();
    ctx.setVariable("one", "This is one");
    final TemplateEngine templateEngine = new TemplateEngine();
    templateEngine.setTemplateResolver(new StringTemplateResolver());
    templateEngine.setLinkBuilder(new TestLinkBuilder());
    templateEngine.setEngineContextFactory(new TestEngineContextFactory());
    final String result = templateEngine.process("<a th:href='@{/something}'>", ctx);
    final String expected = "<a href='[ENGINE: /fromthebuilder/something]'>";
    Assert.assertEquals(expected, result);
}
Also used : Context(org.thymeleaf.context.Context) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) TemplateEngine(org.thymeleaf.TemplateEngine) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) Test(org.junit.Test)

Example 10 with StringTemplateResolver

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

the class TemplateEngineTest method testDefaultTemplateResolver06.

@Test
public void testDefaultTemplateResolver06() {
    final TemplateEngine templateEngine = new SpringTemplateEngine();
    final Context context = new Context();
    context.setLocale(Locale.ENGLISH);
    context.setVariable("one", "this value");
    final StringTemplateResolver stringTemplateResolver = new StringTemplateResolver();
    stringTemplateResolver.setResolvablePatterns(Collections.singleton("<div*"));
    templateEngine.addTemplateResolver(stringTemplateResolver);
    final DefaultTemplateResolver defaultTemplateResolver = new DefaultTemplateResolver();
    defaultTemplateResolver.setTemplate("<p>inserted!</p>");
    templateEngine.addTemplateResolver(defaultTemplateResolver);
    templateEngine.initialize();
    Assert.assertEquals("<div>some text <p><p>inserted!</p></p> other text</div>", templateEngine.process("<div>some text <p th:insert=\"nonexisting\">...</p> other text</div>", context));
}
Also used : Context(org.thymeleaf.context.Context) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) DefaultTemplateResolver(org.thymeleaf.templateresolver.DefaultTemplateResolver) Test(org.junit.Test)

Aggregations

StringTemplateResolver (org.thymeleaf.templateresolver.StringTemplateResolver)11 Context (org.thymeleaf.context.Context)7 Test (org.junit.Test)6 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)6 TemplateEngine (org.thymeleaf.TemplateEngine)5 DefaultTemplateResolver (org.thymeleaf.templateresolver.DefaultTemplateResolver)2 ITemplateResolver (org.thymeleaf.templateresolver.ITemplateResolver)2 StringWriter (java.io.StringWriter)1 LinkedHashSet (java.util.LinkedHashSet)1 PostConstruct (javax.annotation.PostConstruct)1