Resolving template into a string

50 views
Skip to first unread message

Roberto Trunfio

unread,
Apr 10, 2016, 8:51:26 AM4/10/16
to Pebble Templating Engine
This question is similar to the post "Ajax call - Spring Integration".

Background. I'm using the following dependency:

<dependency>
 
<groupId>com.mitchellbosecke</groupId>
 
<artifactId>pebble-spring-boot-starter</artifactId>
 
<version>2.2.1</version>
</dependency>

In a REST controller, I'd like to return an object that includes also a rendered template in the form of a string. I come from a Freemarker experience, where you have an utility class that has a method meant to translate a template into a string.
FreeMarkerTemplateUtils.processTemplateIntoString()

I'm wondering how to do the same in Pebble. Any example?

Roberto Trunfio

unread,
Apr 10, 2016, 9:02:35 AM4/10/16
to Pebble Templating Engine
Ah ok, it was easy:

@Autowired
private PebbleEngine pebbleEngine;


@RequestMapping(value = "/ajax/something/")
public @ResponseBody Object myController(){

 
final PebbleTemplate template = pebbleEngine.getTemplate("my_template");
 
final Writer writer = new StringWriter();
 
template.evaluate(writer, new HashMap<String, Object>());

 
String renderedTemplate = writer.toString();
 
return new MyObject(renderedTemplate);
}


Is there any utility class that encapsulates this?
Reply all
Reply to author
Forward
0 new messages