Grails Cookbook - A collection of tutorials and examples

Grails while Tag Example

The Grails while tag (<g:while>) can be used to render GSP section multiple times. It behaves similar to the while loop in Java/Groovy but just translated to a GSP tag. This will show a simple example on how to use this tag.


Here is a sample code that uses the while tag:

<g:set var="counter" value="${0}"/>
<g:while test="${counter < 5}">
    <g:set var="counter" value="${counter+1}"/>
    <p>The value is ${counter}</p>

This is the output of the code in HTML:

<p>The value is 1</p>
<p>The value is 2</p>
<p>The value is 3</p>
<p>The value is 4</p>
<p>The value is 5</p>

Other Grails Tag Examples

Tags: Grails Tag, while