How to Render MVC Template?

16 views
Skip to first unread message

niko...@brightminds.org

unread,
May 23, 2021, 8:42:25 PM5/23/21
to jooby-project
Hello,

I am using Jooby 2.9.6 and Handlebars.java.

I am trying to render a template to a string so that I can send it as an email yet I cannot seem to find the way to do this.

ModelAndView mvc = new ModelAndView("email_template.hbs");
...
String html = mvc.render()?????

I have found TemplateEngine and MessageEncoder but cannot seem to figure out how to get an instance to be able to render to a string.

This should be so simple... and knowing Jooby probably is ;-)

--Nikolaos

niko...@brightminds.org

unread,
May 25, 2021, 5:48:21 PM5/25/21
to jooby-project
Hello,

Does anyone have a suggestion?  I am sure I am missing something really basic....

I love Jooby and hope others who are using it can assist.

Thank You,

--Nikolaos

István Mészáros

unread,
May 26, 2021, 4:31:34 AM5/26/21
to jooby-project
Hi,


it tells you you can get an instance by:

Handlebars hbs = require(Handlebars.class);


you can see that rendering something can be done by something like this:


@Override public String render(Context ctx, ModelAndView modelAndView) throws Exception {
Template template = handlebars.compile(modelAndView.getView());
Map<String, Object> model = new HashMap<>(ctx.getAttributes());
model.putAll(modelAndView.getModel());
return template.apply(model);
}

regards,
Istvan
Reply all
Reply to author
Forward
0 new messages