【问题标题】:Creating a Custom Conditional TagLib in Grails在 Grails 中创建自定义条件 TagLib
【发布时间】:2011-03-16 14:07:43
【问题描述】:

我正在尝试在 grails 中创建一个条件标记库以确定是否显示用户头像(我基于此处找到的 ifLoggedIn 标记代码:http://www.grails.org/AuthTagLib

我的标签库是这样的:

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

在我的 GSP 中,我使用这样的标签:

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

当我导航到 GSP 时,“找到的头像”正确显示(直接来自 taglib)但“找到了个人资料!”不是。

taglib 中的body{} 没有在 GSP 中显示正文有什么原因吗?

有什么想法可能会出错吗?

谢谢!

【问题讨论】:

    标签: grails groovy taglib


    【解决方案1】:

    body 后面的大括号类型不对,我认为应该是:

    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 {}
      }
    }
    

    更多示例请参见this page in the documentation

    【讨论】:

    • 完美!我错过了正确的大括号和 out
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多