Grails Cookbook - A collection of tutorials and examples

Grails formatBoolean Tag Example

The Grails' <g:formatBoolean> tag can be used to format boolean values for rendering inside GSP view page. Here are some examples on how to use this tag.

Simple Usage

By default, using the tag without customisation will output a formatted value in your GSP page:
  • true
    <g:set var="booleanValue" value="${true}"/>
    <g:formatBoolean boolean="${booleanValue}" />
    
    True
  • false
    <g:set var="booleanValue" value="${false}"/>
    <g:formatBoolean boolean="${booleanValue}" />
    
    False
This is because by default, the messages.properties file have these entries:
default.boolean.true=True
default.boolean.false=False

Customized Label

To customize the output HTML of the tag, just edit the file messages.properties. For example:
default.boolean.true=Good
default.boolean.false=Bad
After changing to the above configuration, the output will now be:
  • true
    <g:set var="booleanValue" value="${true}"/>
    <g:formatBoolean boolean="${booleanValue}" />
    
    Good
  • false
    <g:set var="booleanValue" value="${false}"/>
    <g:formatBoolean boolean="${booleanValue}" />
    
    Bad
Note that we can insert some HTML code to the messages.properties. For example:
default.boolean.true=<span style="color: #ff0000;">Good</span><span style="color: #0000ff;">Sign</span>
default.boolean.false=<span style="color: #ff0000;">Bad</span><span style="color: #0000ff;">Sign</span>
The output will now be colored. Below is the rendered HTML code of the tags:
  • true
    <g:set var="booleanValue" value="${true}"/>
    <g:formatBoolean boolean="${booleanValue}" />
    
    <span style="color: #ff0000;">Good</span><span style="color: #0000ff;">Sign</span>
    
  • false
    <g:set var="booleanValue" value="${false}"/>
    <g:formatBoolean boolean="${booleanValue}" />
    
    <span style="color: #ff0000;">Bad</span><span style="color: #0000ff;">Sign</span>
    
Tags: formatBoolean, Grails Tag