ITeye博客

Writing the <g:if> tag in a tag library

boogie · 2008-06-24 11:51 · Grails · Markdown

The internal if/else tags that come with Grails translate directly
into if(..) {} else {} at the syntax level to improve performance of
such core tags . But you can quite easily write if/else tags using the
pageScope object and Grails' standard tags

for example:

def ifSomething = { attrs, body ->
     pageScope.doElse = !attrs.condition
     if(attrs.condition) out << body()
}
def elseSomething = { attrs, body ->
    if(pageScope.doElse)  out << body()
}

Then

<g:ifSomething condition="${1==2}">yes</g:ifSomething>
<g:elseSomething>no</g:elseSomething>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。