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");
}
}
}
}
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);
}
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);
}
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);
}
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));
}
Aggregations