February 21, 2011

Grails Goodness: Format Boolean Values with the formatBoolean Tag

If we want to display something else than true or false for a boolean value on our Groovy Server Page (GSP) we can use the formatBoolean tag. We can specify a value for true with the true attribute and for false with the false attribute. If we don't specify a true or false attribute Grails will look for the key boolean.true and boolean.false in the i18n resource bundle. If those are not found than the keys default.boolean.true and default.boolean.false are used. And the last fallback are the values True for true and False for false boolean values.

// File: grails-app/i18n/messages.properties
boolean.false=Not okay
<%-- Sample.gsp --%>

<g:formatBoolean boolean="${true}"/> outputs: Okay
<g:formatBoolean boolean="${false}"/> outputs: Not okay

<g:formatBoolean boolean="${true}" true="Yes" false="No"/> outputs: Yes
<g:formatBoolean boolean="${false}" true="Yes" false="No"/> outputs: No