在 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>
正确的做法是什么?现在我有错误。
谢谢,
只是为了干燥:
<%@ page import="grails.util.Environment" %>
<g:if test="${Environment.current in
[Environment.PRODUCTION, Environment.DEVELOPMENT]}">
<h1>xyz</h1>
</g:if>
我发现以下方法可行。
<g:if test="${grails.util.Environment.current.name.equals('development') ||
grails.util.Environment.current.name.equals('production')}">
<h1>xyz</h1>
</g:if>
使用标签库创建您自己的标签。
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>