Grails Template Engine

  • Tags: templating
  • Latest: 0.2.1
  • Last Updated: 13 February 2012
  • Grails version: 1.1 > *
  • Authors: Peter Delahunty
3 votes
Dependency:
compile ":grails-template-engine:0.2.1"

 Documentation

Summary

This plugin exposes the GSP engine as a service. It allows you use the gsp template engine to render gsp out side the standard response.

Installation

grails install-plugin grails-template-engine

Description

Grails Template Engine Plugin

This plugin is probably only useful to Grails applications < 2.0.0. Applications using more a more recent version of Grails should use the
grails.gsp.PageRenderer
introduced in Grails 2.0.0 and/or the
g.render
tag instead of this plugin

Overview

The Grails Template Engine simply exposes the GSP rendering engine as a service. The need for this arose when i wanted to generate an example php file for my customers but wanted to use the GSP engine to create the page.

Compatibility

The plugin is currently compatible with version 1.1 of grails. Not tried it on an earlier version.

Installation

Installation is very simple as is all grails plugin installations;

grails install-plugin grails-template-engine

Documentation

To use the plugin is very simple.

Include the Service

class DemoService {

def grailsTemplateEngineService

def myServiceMethod = {

String text = grailsTemplateEngineService.renderView("/demo/phpFile", [message:"hello"])

// do something with the text

}

}

Controller convenient method

I also added a convenient method to the controllers called renderWithTemplateEngine

def fileDownload = {

String text = renderWithTemplateEngine("/demo/phpFile", [message:"hello"])

response.contentLength = text.size() response.contentType = 'text/plain' response.setHeader("Content-disposition", "attachment; filename=sample.php") response.outputStream << text.bytes response.outputStream.flush() }

The GSP is nothing special

Hello <?php echo urlencode($_GET['name']); ?>

Message: ${message}

Important notes

Make sure you do not set response.contentType to 'text/html' as it will get picked up by the grails standard render engine and will try to encode your file.

Bugs

  • 0.2 - GrailsTemplateEngineService line 45 controllerName should be pluginName. Resolved in version 0.2.1