So let’s say you want to utilize the standard grails tag lib
of formatNumber within your domain class, controller or
service. How would you go about doing it? With the help of a
blog post by Lucas Teixeira, I was able to get it working without
an issue. Here is the code:
It’s also simple to utilize your own tag libs as well. Here’s a tag lib that I created to help with XML formatting:
12345678910111213141516171819
classCommonTagLib{staticnamespace='berry'defformatXML={attrs,body->StringrawXML=body().toString()try{defslurpedXML=newXmlSlurper().parseText(rawXML)defwriter=newStringWriter()newXmlNodePrinter(newPrintWriter(writer)).print(slurpedXML)defformattedResponse=writer.toString()out<<formattedResponse}catch(Exceptione){log.info"Unable to parse XML: ${e.message}"out<<rawXML}}}
So to call this tag lib in the domain class, controller or service, I just need to do the following: