6.3.2 Simple Tags - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
6.3.2 Simple Tags
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
Como se demostro en el ejemplo anterior es facil de escribir etiquetas simples que no tengan cuerpo y solo contenido de salida. Otro ejemplo es la etiqueta de estilo dateFormat style tag:
dateFormat:def dateFormat = { attrs, body ->
out << new java.text.SimpleDateFormat(attrs.format).format(attrs.date)
}
The above uses Java's
El codigo de arriba usa la clase de Java SimpleDateFormat class to format a date and then write it to the response. The tag can then be used within a GSP as follows:
SimpleDateFormat para dar el formato a una fecha y entonces escribirla en la respuesta. La etiqueta puede entonces ser usada dentro del GSP como sigue:<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:
Con las etiquetas simples a veces necesitara escribir HTML mark-up en la respuesta. Una propuesta podria ser embeber el contenido directamente: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 reuse the render tag:
A pesar que este propuesta pueda ser tentativa, no es muy limpia. Una propuesta mejor seria el reusar la etiqueta render:def formatBook = { attrs, body ->
out << render(template: "bookTemplate", model: [book: attrs.book])
}
And then have a separate GSP template that does the actual rendering.
Y entonces tener una plantilla de GSP separada que haga el rendering actual.

