Platinum Partner
groovy,grails

Detecting the Grails Environment in a GSP

Last night I needed to make sure that my analytics tracking script was not running anywhere but in my production environment. After reading another blog post I came up with this simple GSP if block.

**** UPDATE ****

Thanks Burt for the comment pointing out that the original code I posted is actually deprecated and for pointing out a better way for testing environment.

According to the docs (and Burt), this is the correct way to test for environment in a GSP.

<g:if env="production">
<!-- Markup to include ONLY when in production -->
</g:if>

**** The code below is deprecated ****

<g:if test="${ grails.util.GrailsUtil.getEnvironment().equals(org.codehaus.groovy.grails.commons.GrailsApplication.ENV_PRODUCTION) }">
<!-- Markup to include ONLY when in production -->
</g:if>

Hope this helps others who are looking to do the same within their GSPs.

Detecting the Grails Environment in a GSP

Published at DZone with permission of {{ articles[0].authors[0].realName }}, DZone MVB. (source)

Opinions expressed by DZone contributors are their own.

{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}