【问题标题】:decorate Grails tag装饰 Grails 标签
【发布时间】:2011-08-09 08:42:25
【问题描述】:

Grails bean fields 插件提供的 taglib 使用命名约定来确定它生成的每个 <input> 元素应该使用的标签键。我想在不直接更改插件源代码的情况下更改此约定的细节。

我正在考虑的方法是创建自己的标签库

class MyBeanTagLib {

  static namespace = 'mybean'

  private void setLabelKey (attrs) {

    if (!attrs.labelKey) {

      // in reality calculation of the default key is a bit more complicated :)
      attrs.labelKey = 'my.default.key'
    }
    return attrs
  }

  // renders a combo box
  def select = { attrs ->
     attrs = setLabelKey(attrs)

     // Now call the bean-fields select tag, passing along attrs     
  }

  // renders a datePicker
  def date = { attrs -
     attrs = setLabelKey(attrs)

     // Now call the bean-fields date tag, passing along attrs     
  }
}

我的第一个问题是如何调用我要装饰的标签。换句话说,应该用什么代码代替注释

// 现在调用 bean-fields...

我可以这样做:

new BeanTagLib().select(attrs)

但我怀疑这是从另一个标签库调用一个标签库的正确方法。

其次,有没有比这更优雅的方式来装饰标签库?实际上,除了selectdate 之外,我需要装饰的标签还有很多,每个装饰标签中的代码几乎相同。如果可能,我想消除这种重复?

【问题讨论】:

    标签: grails groovy grails-plugin taglib


    【解决方案1】:
    1. 通过命名空间调用其他标签库的标签,例如g.link([controller: 'one'], { 'link text' })bean.select(attrs)

    2. 您可以尝试在 taglib 中编写 getProperty 并返回正确的闭包 - 我不确定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多