How to Programmatically resolve Thymeleaf templates?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

How to Programmatically resolve Thymeleaf templates?

asarkar
I'm trying to render XML/JSON using Thymeleaf templates. I don't want to render a view using the template name, just want to resolve the template as shown below. Trouble is all I get back is the template name, not it's content.

Set up:

@Bean
SpringResourceTemplateResolver xmlTemplateResolver(ApplicationContext appCtx) {
    SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();

    templateResolver.setApplicationContext(appCtx);
    templateResolver.setPrefix("classpath:/templates/");
    templateResolver.setSuffix(".xml");
    templateResolver.setTemplateMode(XML);
    templateResolver.setCharacterEncoding(UTF_8.name());
    templateResolver.setCacheable(false);

    return templateResolver;
}

@Bean
SpringTemplateEngine templateEngine(ApplicationContext appCtx) {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.addTemplateResolver(xmlTemplateResolver(appCtx));

    return templateEngine;
}

Template (src/main/resources/templates/breakfast-menu.xml): See attached image as the XML is mangled by formatting. Template Usage:
@Autowired
SpringTemplateEngine templateEngine;

someMethod() {
    Context context = new Context();
    context.setVariable("item", item);
    item.put("name", "Waffle");
    String content = templateEngine.process("breakfast-menu", context);

    // content == "breakfast-menu". WTH?
}
Using Thymeleaf 3.0.0.BETA01.
Reply | Threaded
Open this post in threaded view
|

Re: How to Programmatically resolve Thymeleaf templates?

Zemi
Administrator
Hello,

please try to replace
  templateEngine.addTemplateResolver()
by
  templateEngine.setTemplateResolver()

I don't know the details of it, but it happened to me before.

Regards,
  Zemi

Reply | Threaded
Open this post in threaded view
|

Re: How to Programmatically resolve Thymeleaf templates?

asarkar
@Zemi,
With that change, I'm getting the content now. However, the placeholders are not replaced in the template. WHat's the correct format to use data from a map?
Reply | Threaded
Open this post in threaded view
|

Re: How to Programmatically resolve Thymeleaf templates?

Zemi
Administrator
Hello,

you  can use, for example,

  <name th:text="${item['name']}"></name>

Regards,
  Zemi

Reply | Threaded
Open this post in threaded view
|

Re: How to Programmatically resolve Thymeleaf templates?

asarkar
I got the following working for XML but I'm not sure how to do JSON, which is plain text, and has no concept of namespace declaration.

Reply | Threaded
Open this post in threaded view
|

Re: How to Programmatically resolve Thymeleaf templates?

Zemi
Administrator
For JSON you will need to use the TEXT mode and the different textual syntaxes, please see
  http://www.thymeleaf.org/doc/articles/thymeleaf3migration.html

Regards,
  Zemi

Reply | Threaded
Open this post in threaded view
|

Re: How to Programmatically resolve Thymeleaf templates?

asarkar
OK, I got the following working for JSON too. Hope it helps anyone else having similar questions.



One last question: If I've both XML and JSON templates in the same directory and they're similarly named (except extension), can I choose one at runtime? Currently I've both resolvers but the first one wins.
Reply | Threaded
Open this post in threaded view
|

Re: How to Programmatically resolve Thymeleaf templates?

Zemi
Administrator
Hi,

please note that you can use this more concise syntax
  [[${item['name']}]]

About the templates, you can set an "order" property to any resolver to prioritize the order they pick up the templates. If you have all of them in the same template, maybe it's better not to configure a suffix and put the suffix in the template name.

Regards,
   Zemi

Reply | Threaded
Open this post in threaded view
|

Re: How to Programmatically resolve Thymeleaf templates?

asarkar
Thank you.