Thursday, May 22, 2008

Resource File Loading in Grails

Loading a resource file using Grails isn't that obvious when you first attempt to do it. I finally figured out how to do that using the follwing code:

public String buildTemplate(def someBinding){
def template = ApplicationHolder.application.
parentContext.getResource("classpath:template.tmpl")
def engine = new SimpleTemplateEngine()
def template = engine.createTemplate(template.getFile()).make(someBinding)
return template.toString()
}

Obviously an import is needed to run the above code successfully

import org.codehaus.groovy.grails.commons.ApplicationHolder

No comments: