Search in sources :

Example 1 with StringTemplateResolver

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

the class LinkBuilderTest method testLinkBuilder01.

@Test
public void testLinkBuilder01() 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());
    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) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) Test(org.junit.Test)

Example 2 with StringTemplateResolver

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

the class LinkBuilderTest method testLinkBuilderSpringWithECFactory01.

@Test
public void testLinkBuilderSpringWithECFactory01() 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());
    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) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) Test(org.junit.Test)

Example 3 with StringTemplateResolver

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

the class TemplateEngineTest method testDefaultTemplateResolver05.

@Test
public void testDefaultTemplateResolver05() {
    final TemplateEngine templateEngine = new TemplateEngine();
    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) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) DefaultTemplateResolver(org.thymeleaf.templateresolver.DefaultTemplateResolver) Test(org.junit.Test)

Example 4 with StringTemplateResolver

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

the class ThymeleafConfig method getStringTemplateResolver.

private StringTemplateResolver getStringTemplateResolver() {
    final StringTemplateResolver templateResolver = new StringTemplateResolver();
    templateResolver.setTemplateMode(TemplateMode.HTML);
    templateResolver.setCacheable(false);
    return templateResolver;
}
Also used : StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver)

Example 5 with StringTemplateResolver

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

the class ThymeleafConfig method postConstruct.

@PostConstruct
private void postConstruct() {
    Set<ITemplateResolver> resolverSet = new LinkedHashSet<>();
    // At first, try to load template from resources by given name
    defaultTemplateResolver.setOrder(0);
    resolverSet.add(defaultTemplateResolver);
    // If there was founf no such template, use the string as template by itself
    StringTemplateResolver stringTemplateResolver = getStringTemplateResolver();
    stringTemplateResolver.setOrder(1);
    resolverSet.add(stringTemplateResolver);
    templateEngine.setTemplateResolvers(resolverSet);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver) PostConstruct(javax.annotation.PostConstruct)

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