Mostrando entradas con la etiqueta nl2br break line br grails. Mostrar todas las entradas
Mostrando entradas con la etiqueta nl2br break line br grails. Mostrar todas las entradas

domingo, 31 de mayo de 2009

nl2br in grails, english version

PHP has one function that converts String's breaklines "/n" into <br> html tags.

Grails doesn't have a similar function to format Strings this way, but we can make our own Grail's codec, in order to obtain the same result as in PHP:

1. Create your own codec
2. Call it directly from the GSP page

1. To create the new codec, you just need to create a Grail's class in the folder grails-app/utils/ , like this:

NL2BRCodec.groovy (The file has to be named with the 'Codec' sufix)

class NL2BRCodec{
static encode = { string ->
string.trim().replaceAll("\n","<br>")
}
}

That's all, now, from the GSP page, you just have to call the codec like this:

<g:encodeAs codec="NL2BR">${object.stringValue}</g:encodeAs>

Or:

${object.stringValue.encodeAsNL2BR()}