Login required
Download

Grails Template Engine

(2)
Author(s): Peter Delahunty
Current Release: 0.2
Grails Version: 1.1 > *
Tags templating
grails install-plugin grails-template-engine

Grails Template Engine 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 {

GrailsTemplateEngineService grailsTemplateEngineService

def myServiceMethod = {

String text = grailsTemplateEngineService.renderWithTemplateEngine("/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