Here is a simple way to use the GSP from Grails to build reusable templates from files coming from the outside of the application (for mails, files exports, etc. …)

The first step is to create a GSP which defines the template. It should be noted that no special tag will be interpreted this way. Here is a sample VCARD file to import a contact (_vcard.gsp) :
BEGIN:VCARD
VERSION:2.1
N:${people.lastname};${people.firstname};;${people.civility}
FN:${people.civility} ${people.firstname} ${people.lastname}
TITLE:${people.job}
TEL;WORK;VOICE:${people.professionnalPhone}
EMAIL;INTERNET:${people.email}
END:VCARD

Then we must add a small method that will link the template to the data through the Groovy’s template engine :

class MyUtilityClass {
static SimpleTemplateEngine engine = new SimpleTemplateEngine()
static generateFromGSP(String templateFilename, model) throws Exception {
Template template = engine.createTemplate(new File(templateFilename))
return template.make(model).toString()
}
}

And finally all that remains is a call from a piece of Groovy code, in a controller for example :
def myVcard
People myContact = People.findByName(’lorenzo’)
myVcard = MyUtilityClass.generateFromGSP(”_vcard.gsp”,['people':myContact])

That’s all.. The myVcard variable contains the template supplied with data from myContact, we can now send it to a web browser to download it for example ..

This is not necessarily highlighted in the documentation, but the use of GSP from the inside of the Grails application grails to create templates is even easier.
Just do the same way as from the inside of a classical GSP, as the methods of the taglib are available. Here is the same result as the example above, but with a GSP located in the project :
People myContact = People.findByName(’lorenzo’)
vcard = g.render(template: “/myTemplates/vcard”, model: ['people':myContact])