Question

I am learning grails using the definitive guide to grails 2 (using grails 2.3.7), and when I'm looking at the custom tag library it gives an example custom tag as follows:

    def repeat = { attrs, body ->
        int n = attrs.int('times)
        n?.times { counter ->
              out << body(counter +1)
        }
    }

so when I use this tag like so:

   <g:repeat times="3">
      Hello number ${it}<br>
   </g:repeat>

I expect to get three separate lines on my rendered HTML:

   Hello number 1
   Hello number 2     
   Hello number 3

Instead I get:

   hello number 1<br>hello number 2<br>hello number 3<br>

I have found methods that look like they should help, like decodeHTML() however I am thus unable to change the output that I want, and I'm not sure what I'm doing wrong.

I have tried doing:

   out <<body.decodeHTML()

but I get a null pointer error...

Was it helpful?

Solution

That does not make sense unless there is something else in your taglib or something unusual in the GSP which is invoking the tag.

Does your taglib maybe have something like defaultEncodeAs='html' in it?

OTHER TIPS

Use the tag like this:

<g:repeat times="3">
    Hello number ${it}<br/>
</g:repeat>

HTML5 can render <br>, but it seems you are using version 4 or lower.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top