Grails Cookbook - A collection of tutorials and examples

Grails elseif Tag Example

The Grails elseif tag (<g:elseif>) tag is used together with the if tag (<g:if>) and optionally with the else tag (<g:else>). The elseif is used if there are more than one test that needs to be performed. Here is a simple example on how to use this tag.


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

class TestController {
    def index() {
Here is a GSP code that uses both if, elseif, and else tags:
<g:if test="${num==0}">
    The number ${num} is zero.
<g:elseif test="${num<0}">
    The number ${num} is negative.
    The number ${num} is positive.

Notice that there are 2 test condition and 3 possible outcome.

The expected output is:

The number -50 is negative. 

Other Grails Tag Examples

Tags: elseif, Grails Tag