Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Custom Conditional TagLib in Grails

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

My taglib looks like this:

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 like this:

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

When I navigate to the GSP, "avatar found" is being displayed correctly (directly from the taglib) but "profile found!" is not.

Is there any reason that the body{} in the taglib is not showing the body in the GSP?

Any ideas where it might be going wrong?

Thanks!

like image 913
rhinds Avatar asked Dec 15 '25 12:12

rhinds


1 Answers

Wrong sort of 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 examples

like image 69
tim_yates Avatar answered Dec 18 '25 00:12

tim_yates



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!