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