(Quick Reference)

6.3.3 逻辑标签 - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

6.3.3 逻辑标签

You can also create logical tags where the body of the tag is only output once a set of conditions have been met. An example of this may be a set of security tags:

def isAdmin = { attrs, body ->
    def user = attrs.user
    if (user && checkUserPrivs(user)) {
        out << body()
    }
}

The tag above checks if the user is an administrator and only outputs the body content if he/she has the correct set of access privileges:

<g:isAdmin user="${myUser}">
    // some restricted content
</g:isAdmin>

你也可以创建一个逻辑标签,一旦一组条件表达式满足,就输出标签的主体。一组安全标签的示例如下:

def isAdmin = { attrs, body ->
    def user = attrs.user
    if (user && checkUserPrivs(user)) {
        out << body()
    }
}

上述标签将检查用户是否为一个管理员,并且只有他/她有正确的访问权限的时候,才可以输出主体内容:

<g:isAdmin user="${myUser}">
    // some restricted content
</g:isAdmin>