<g:if>逻辑或条件

问题描述 投票:0回答:3

在 grails gsp 中,而不是

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

是否可以写入逻辑或条件来组合两个条件

例如

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

正确的做法是什么?现在我有错误。

谢谢,

grails gsp
3个回答
8
投票

只是为了干燥:

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

1
投票

我发现以下方法可行。

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

0
投票

使用标签库创建您自己的标签。

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()
            }
        }
    }
}

然后在您的 GPS 中使用它。

<env:is in="['DEVELOPMENT', 'PRODUCTION']>
    <div>Stuff here</div>
</env:is>
© www.soinside.com 2019 - 2024. All rights reserved.