如何将自定义taglib标记的出口限制为每页一个?

时间:2011-05-19 05:17:15

标签: grails tags taglib

例如,当我希望<custom:footer/>无法在一个页面上多次出现时。

1 个答案:

答案 0 :(得分:1)

每次调用标记时,都要在页面上下文中设置一个属性。如果该属性已存在,则表示已为当前页面调用了标记,因此抛出异常。像这样(未经测试)

class MyTagLib {

   def doIt = { attrs, body ->

       if (pageScope.invoked) {
           // throwTagError is a built-in method that is available in all tag libs
           thowTagError "this tag can only be invoked once per page"
       }

       pageScope.invoked = true

      // Now cure world poverty or whatever it is that your tag is supposed to do
   }
}