Grails Cookbook - A collection of tutorials and examples

Grails if Tag Example

The Grails if tag (<g:if>) renders it's body when the condition given is true. This will show a simple example on how to use this tag.

Examples

TEST

This code:
<g:set var="salary" value="${1000}"/>
<g:unless test="${salary == 1000}">
    Hello World
</g:unless>
Will render the body "Hello World"
But this code:
<g:set var="salary" value="${1000}"/>
<g:unless test="${salary != 1000}">
    Hello World
</g:unless>
Will NOT render the body "Hello World"

ENVIRONMENT

It is possible to check the current environment where the application is running. (E.g. development, production)
The development environment is true when you run the application using the command "grails run-app". The production environment is true when you package the application as WAR and deploy it to an application server or servlet container.
This code will render what is the current environment.
<g:if env="development">
    Hello, you are in development environment
</g:if>
<g:if env="production">
    Hello, you are in production environment
</g:if>

ENVIRONMENT + TEST

It is possible to specify both test and environment. Both must be satisfied to render the body of the tag.

This code:

<g:if env="production" test="${salary > 10000}">
    Hello, you are in production environment
</g:if>

Will only render the body when current environment is production and the salary is greater than 10000.

Other Grails Tag Examples


Tags: Grails Tag, if