Search in sources :

Example 1 with DefaultTemplateResolver

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

the class TemplateEngineTest method testDefaultTemplateResolver02.

@Test
public void testDefaultTemplateResolver02() {
    final TemplateEngine templateEngine = new SpringTemplateEngine();
    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>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>something</p>", templateEngine.process("nonexisting", context));
}
Also used : Context(org.thymeleaf.context.Context) SpringTemplateEngine(org.thymeleaf.spring5.SpringTemplateEngine) 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)

Example 2 with DefaultTemplateResolver

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

the class TemplateEngineTest method testDefaultTemplateResolver01.

@Test
public void testDefaultTemplateResolver01() {
    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>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>something</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)

Example 3 with DefaultTemplateResolver

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

the class TemplateEngineTest method testDefaultTemplateResolver04.

@Test
public void testDefaultTemplateResolver04() {
    final TemplateEngine templateEngine = new SpringTemplateEngine();
    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) 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)

Example 4 with DefaultTemplateResolver

use of org.thymeleaf.templateresolver.DefaultTemplateResolver 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 5 with DefaultTemplateResolver

use of org.thymeleaf.templateresolver.DefaultTemplateResolver in project hutool by looly.

the class ThymeleafEngine method createEngine.

/**
 * 创建引擎
 *
 * @param config 模板配置
 * @return {@link TemplateEngine}
 */
private static org.thymeleaf.TemplateEngine createEngine(TemplateConfig config) {
    if (null == config) {
        config = new TemplateConfig();
    }
    ITemplateResolver resolver;
    switch(config.getResourceMode()) {
        case CLASSPATH:
            final ClassLoaderTemplateResolver classLoaderResolver = new ClassLoaderTemplateResolver();
            classLoaderResolver.setCharacterEncoding(config.getCharsetStr());
            classLoaderResolver.setTemplateMode(TemplateMode.HTML);
            classLoaderResolver.setPrefix(StrUtil.addSuffixIfNot(config.getPath(), "/"));
            resolver = classLoaderResolver;
            break;
        case FILE:
            final FileTemplateResolver fileResolver = new FileTemplateResolver();
            fileResolver.setCharacterEncoding(config.getCharsetStr());
            fileResolver.setTemplateMode(TemplateMode.HTML);
            fileResolver.setPrefix(StrUtil.addSuffixIfNot(config.getPath(), "/"));
            resolver = fileResolver;
            break;
        case WEB_ROOT:
            final FileTemplateResolver webRootResolver = new FileTemplateResolver();
            webRootResolver.setCharacterEncoding(config.getCharsetStr());
            webRootResolver.setTemplateMode(TemplateMode.HTML);
            webRootResolver.setPrefix(StrUtil.addSuffixIfNot(FileUtil.getAbsolutePath(FileUtil.file(FileUtil.getWebRoot(), config.getPath())), "/"));
            resolver = webRootResolver;
            break;
        case STRING:
            resolver = new StringTemplateResolver();
            break;
        default:
            resolver = new DefaultTemplateResolver();
            break;
    }
    final org.thymeleaf.TemplateEngine engine = new org.thymeleaf.TemplateEngine();
    engine.setTemplateResolver(resolver);
    return engine;
}
Also used : ClassLoaderTemplateResolver(org.thymeleaf.templateresolver.ClassLoaderTemplateResolver) TemplateEngine(cn.hutool.extra.template.TemplateEngine) StringTemplateResolver(org.thymeleaf.templateresolver.StringTemplateResolver) FileTemplateResolver(org.thymeleaf.templateresolver.FileTemplateResolver) TemplateConfig(cn.hutool.extra.template.TemplateConfig) DefaultTemplateResolver(org.thymeleaf.templateresolver.DefaultTemplateResolver) ITemplateResolver(org.thymeleaf.templateresolver.ITemplateResolver)

Aggregations

DefaultTemplateResolver (org.thymeleaf.templateresolver.DefaultTemplateResolver)7 Test (org.junit.Test)6 Context (org.thymeleaf.context.Context)6 SpringTemplateEngine (org.thymeleaf.spring5.SpringTemplateEngine)6 ClassLoaderTemplateResolver (org.thymeleaf.templateresolver.ClassLoaderTemplateResolver)5 ITemplateResolver (org.thymeleaf.templateresolver.ITemplateResolver)5 ArrayList (java.util.ArrayList)4 StringTemplateResolver (org.thymeleaf.templateresolver.StringTemplateResolver)3 TemplateConfig (cn.hutool.extra.template.TemplateConfig)1 TemplateEngine (cn.hutool.extra.template.TemplateEngine)1 FileTemplateResolver (org.thymeleaf.templateresolver.FileTemplateResolver)1