【发布时间】: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)
但我怀疑这是从另一个标签库调用一个标签库的正确方法。
其次,有没有比这更优雅的方式来装饰标签库?实际上,除了select 和date 之外,我需要装饰的标签还有很多,每个装饰标签中的代码几乎相同。如果可能,我想消除这种重复?
【问题讨论】:
标签: grails groovy grails-plugin taglib