As demonstrated it the previous example it is trivial to write simple tags that have no body and merely output content. Another example is a
dateFormat
style tag:
def dateFormat = { attrs, body ->
out << new java.text.SimpleDateFormat(attrs.format).format(attrs.date)
}
The above uses Java's
SimpleDateFormat
class to format a date and then write it to the response. The tag can then be used within a GSP as follows:
<g:dateFormat format="dd-MM-yyyy" date="${new Date()}" />
With simple tags sometimes you need to write HTML mark-up to the response. One approach would be to embed the content directly:
def formatBook = { attrs, body ->
out << "<div id="${attrs.book.id}">"
out << "Title : ${attrs.book.title}"
out << "</div>"
}
Although this approach may be tempting it is not very clean. A better approach would be to re-use the
render tag:
def formatBook = { attrs, body ->
out << render(template:"bookTemplate", model:[book:attrs.book])
}
And then have a separate GSP template that does the actual rendering.