Creating a Custom Conditional TagLib in Grails

I am trying to create a conditional taglib in grails to determine if the user should show the Avatar (I based the code on ifLoggedIn tags found here: http://www.grails.org/AuthTagLib )

My taglib is as follows:

def ifProfileAvatar = {attrs, body ->
  def username = session.user.login
  def currentUser = Account.findByLogin(username)
  if (currentUser.profile && currentUser.profile.avatar) {
    out << "avatar found"
    body{}
  }
}

And in my GSP, I use the tag as follows:

<g:ifProfileAvatar>
<br/>profile found!<br/>
</g:ifProfileAvatar>

When I go to GSP, "avatar found" displays correctly (directly from taglib), but "profile found!" no.

Is there a reason body{}taglib doesn't show body in GSP?

Any ideas where this might go wrong?

Thank!

+3
source share
1 answer

The wrong appearance of curly braces after body, I think it should be:

def ifProfileAvatar = {attrs, body ->
  def username = session.user.login
  def currentUser = Account.findByLogin(username)
  if (currentUser.profile && currentUser.profile.avatar) {
    out << "avatar found"
    out << body() // Use () not {}
  }
}

See this page in the documentation for more details .

+10
source

All Articles