use of ratpack.groovy.template.MarkupTemplate in project ratpack by ratpack.
the class MarkupTemplateRenderer method render.
@Override
public void render(Context ctx, MarkupTemplate template) throws Exception {
String contentType = template.getContentType();
contentType = contentType == null ? ctx.get(MimeTypes.class).getContentType(template.getName()) : contentType;
try {
Template compiledTemplate = engine.createTemplateByPath(template.getName());
Writable boundTemplate = compiledTemplate.make(template.getModel());
ByteBuf byteBuf = byteBufAllocator.directBuffer();
try {
OutputStream outputStream = new ByteBufOutputStream(byteBuf);
Writer writer = new OutputStreamWriter(outputStream, CharsetUtil.encoder(StandardCharsets.UTF_8));
boundTemplate.writeTo(writer);
} catch (Exception e) {
byteBuf.release();
throw e;
}
ctx.getResponse().send(contentType, byteBuf);
} catch (IOException e) {
ctx.error(e);
}
}
Aggregations