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.

Example

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

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

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