Grails Cookbook - A collection of tutorials and examples

Grails else Tag Example

The Grails else tag (<g:else>) tag is used together with the if tag (<g:if>). The else portion is rendered when the if part failed. This is used to have conditional statements inside a GSP. This is useful if we want to display a certain view code or another depending on some condition. here is a simple example on how to use the Grails else tag.

Example

Here is a controller code that will pass a number to a GSP:

class TestController {
    def index() {
        [num:100]
    }
}
Here is a GSP code that uses both if and else tags:
<g:if test="${num<0}">
   The number ${num} is negative.
</g:if>
<g:else>
   The number ${num} is not negative.
</g:else>

The expected output is:

 The number 100 is not negative. 


Other Grails Tag Examples


Tags: else, Grails Tag