(Quick Reference)

3 Rendering - Reference Documentation

Authors: Grails Plugin Collective

Version: 1.0.0

3 Rendering

There are four services available for rendering:
  • pdfRenderingService
  • gifRenderingService
  • pngRenderingService
  • jpegRenderingService

All services have the same method…

OutputStream render(Map args, OutputStream destination = new ByteArrayOutputStream())

The args define the render operation, with the bytes written to the given output stream. The given output stream is returned from the method. If no destination is provided, the render will write to a ByteArrayOutputStream that is returned.

Here are some examples:

// Get the bytes
def bytes = gifRenderingService.render(template: '/images/coupon', model: [serial: 12345])

// Render to a file new File("coupon.jpg").withOutputStream { outputStream -> jpegRenderingService.render([template: '/images/coupon', model: [serial: 12345]], outputStream) }

For information on rendering to the HTTP response, see Rendering To The Response.

Basic Render Arguments

All rendering methods take a Map argument that specifies which template to render and the model to use (in most cases).

The following map arguments are common to all rendering methods:

  • template (required) - The template to render
  • model (optional) - The model to use
  • plugin (optional) - The plug-in containing the template
  • controller (optional) - The controller instance or name to resolve the template against (set automatically in provided render«format» methods on controllers).

Template Resolution

The plugin uses the same resolution strategy as the render() method in Grails controllers and taglibs.

That is,

  • template files must start with an underscore (_template.gsp)
  • template paths starting with "/" are resolved relative to the views directory
  • template paths NOT starting with "/" are resolved relative to the views/«controller» directory

If the template argument does not start with a "/", the controller argument must be provided. The methods added to controllers (e.g. renderPdf()) automatically pass the controller param for you.