Loops are one of the most important and useful part of every programming language. Grails provides various loops in controller and view. Following are the loops in GSP:
- each: each tag is used to iterate over each element of a list.
<g:each in="${books}" status="i"> <p>Index: ${i}</p> <p>Title: ${it.title}</p> <p>Author: ${it.author}</p> </g:each>
where
- in – The object to iterate over
- status (optional) – The name of a variable to store the iteration index in.
- var (optional) – The name of the item. Defaults “it”.
- while: while is used to executes a condition until the condition returns false.
<g:while test="${i < 5}"> <%i++%> <p>Current i = ${i}</p> </g:while>
where
- test – The conditional expression
- unless: unless is used to renders its body unless all of the specified conditions (test and/or environment) are true.
<g:unless test="${name == 'fred'}"> Hello ${name}! </g:unless> <g:unless env="production"> debug: $someDebug </g:unless>
where
- test – The expression to test
- env – An environment name
Note:- If both are supplied, they are combined using AND.
Hope this helps 🙂 .
Recent Comments