Search in sources :

Example 1 with MarkupTemplate

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);
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStream(java.io.OutputStream) ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) Writable(groovy.lang.Writable) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) MimeTypes(ratpack.file.MimeTypes) ByteBuf(io.netty.buffer.ByteBuf) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) MarkupTemplate(ratpack.groovy.template.MarkupTemplate) Template(groovy.text.Template)

Aggregations

Writable (groovy.lang.Writable)1 Template (groovy.text.Template)1 ByteBuf (io.netty.buffer.ByteBuf)1 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 MimeTypes (ratpack.file.MimeTypes)1 MarkupTemplate (ratpack.groovy.template.MarkupTemplate)1