6.3.2 简单标签 - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
6.3.2 简单标签
As demonstrated it the previous example it is easy to write simple tags that have no body and just output content. Another example is a The above uses Java's With simple tags sometimes you need to write HTML mark-up to the response. One approach would be to embed the content directly:Although this approach may be tempting it is not very clean. A better approach would be to reuse the render tag:And then have a separate GSP template that does the actual rendering.
正如以前示例所演示的那样,要写一个只输出内容而没有主体(body)的标签是很容易的。另外的一个示例是dateFormat style tag:def dateFormat = { attrs, body ->
out << new java.text.SimpleDateFormat(attrs.format).format(attrs.date)
}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()}" />def formatBook = { attrs, body ->
out << "<div id="${attrs.book.id}">"
out << "Title : ${attrs.book.title}"
out << "</div>"
}def formatBook = { attrs, body ->
out << render(template: "bookTemplate", model: [book: attrs.book])
}dateFormat风格的标签:def dateFormat = { attrs, body ->
out << new java.text.SimpleDateFormat(attrs.format).format(attrs.date)
}SimpleDateFormat类来格式化一个日期,并且将它写回到响应中。然后标签就可以在GSP中像下面所示那样使用:<g:dateFormat format="dd-MM-yyyy" date="${new Date()}" />def formatBook = { attrs, body ->
out << "<div id="${attrs.book.id}">"
out << "Title : ${attrs.book.title}"
out << "</div>"
}def formatBook = { attrs, body ->
out << render(template: "bookTemplate", model: [book: attrs.book])
}
