johnsam
johnsam

Reputation: 4572

<g:if> logical or condition

In grails gsp, instead of

<g:if env="development">
     <h1> xyz </h1>
</g:if>
<g:if env="production">
     <h1> xyz </h1>
</g:if>

is it possible to write logical or condition in <g:if> to combine the two conditions

for example

<g:if test="env='production'"||"env='devlopment'">
    <h1> xyz </h1>
</g:if>

What's the right way of doing that?

Upvotes: 4

Views: 3132

Answers (3)

Nick
Nick

Reputation: 1269

Create you're own tags with a taglib.

import grails.util.Environment

class EnvTagLib {
    static namespace = 'env'
    
    def is = { attrs, body ->
        if (attrs.in instanceof String) {
            if (Environment.current.getName() == attrs.in?.toLowerCase()) {
                out << body()
            }
        } else if (attrs.in instanceof List) {
            if (Environment.current.getName() in attrs.in?.collect{ it.toLowerCase()}) {
                out << body()
            }
        }
    }
}

Then use it in your gsps.

<env:is in="['DEVELOPMENT', 'PRODUCTION']>
    <div>Stuff here</div>
</env:is>

Upvotes: 0

johnsam
johnsam

Reputation: 4572

I found the following would work.

<g:if test="${grails.util.Environment.current.name.equals('development') ||                      
              grails.util.Environment.current.name.equals('production')}">
        <h1>xyz</h1>
</g:if>

Upvotes: 1

dmahapatro
dmahapatro

Reputation: 50265

Just for the sake of DRYness:

<%@ page import="grails.util.Environment" %> 
<g:if test="${Environment.current in 
                [Environment.PRODUCTION, Environment.DEVELOPMENT]}">
    <h1>xyz</h1>
</g:if>

Upvotes: 8

Related Questions