Skip to content

Instantly share code, notes, and snippets.

@melix
Created February 25, 2014 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melix/9213889 to your computer and use it in GitHub Desktop.
Save melix/9213889 to your computer and use it in GitHub Desktop.
Reuse of Grails taglibs in the markup template engine
void testGrailsTagLibCompatibility() {
TemplateConfiguration config = new TemplateConfiguration()
MarkupTemplateEngine engine = new MarkupTemplateEngine(this.class.classLoader, config)
def template = engine.createTemplate '''g.emoticon(happy:'true') { 'Hi John' }
'''
StringWriter rendered = new StringWriter()
def model = [:]
def tpl = template.make(model)
model.g = new TagLibAdapter(tpl)
model.g.registerTagLib(SimpleTagLib)
tpl.writeTo(rendered)
assert rendered.toString() == "Hi John :-)"
}
class SimpleTagLib {
def emoticon = { attrs, body ->
out << body() << (attrs.happy == 'true' ? " :-)" : " :-(")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment