8.3.4 Iterative Tags - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 3.1.9
8.3.4 Iterative Tags
Iterative tags are easy too, since you can invoke the body multiple times:def repeat = { attrs, body ->
attrs.times?.toInteger()?.times { num ->
out << body(num)
}
}times attribute and if it exists convert it to a number, then use Groovy's times method to iterate the specified number of times:<g:repeat times="3"> <p>Repeat this 3 times! Current repeat = ${it}</p> </g:repeat>
it variable to refer to the current number. This works because when we invoked the body we passed in the current value inside the iteration:out << body(num)
it to the tag. However, if you have nested tags this can lead to conflicts, so you should instead name the variables that the body uses:def repeat = { attrs, body ->
def var = attrs.var ?: "num"
attrs.times?.toInteger()?.times { num ->
out << body((var):num)
}
}var attribute and if there is use that as the name to pass into the body invocation on this line:out << body((var):num)Note the usage of the parenthesis around the variable name. If you omit these Groovy assumes you are using a String key and not referring to the variable itself.Now we can change the usage of the tag as follows:
<g:repeat times="3" var="j"> <p>Repeat this 3 times! Current repeat = ${j}</p> </g:repeat>
var attribute to define the name of the variable j and then we are able to reference that variable within the body of the tag.
