Created
February 25, 2014 17:40
-
-
Save melix/9213889 to your computer and use it in GitHub Desktop.
Reuse of Grails taglibs in the markup template engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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