装饰 Grails 标签
Grails bean fields 插件提供的 taglib 使用命名约定来确定标签键应该用于它生成的每个 元素。我想更改此约定的详细信息,而不直接更改插件的源代码。
我正在考虑的方法是创建我自己的标签库
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
,而且每个装饰标签中的代码几乎是相同的。如果可能的话,我想消除这种重复?
The taglib provided by the Grails bean fields plugin uses a naming convention to determine the label key that should be used for each <input>
element it generates. I would like to change the details of this convention without changing the plugin's source code directly.
The approach I'm considering is to create my own tag lib
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
}
}
My first question is how to invoke the tag I'm trying to decorate. In other words, what code should replace the comment
// Now call the bean-fields...
I could do this:
new BeanTagLib().select(attrs)
But I doubt this is the correct way to invoke one taglib from another.
Secondly, is there a more elegant way to decorate a taglib than this? In reality there are a lot more tags than just select
and date
that I need to decorate and the code in each decorating tag will be almost identical. I'd like to eliminate this duplication if possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过名称空间调用其他标签库的标签,例如
g.link([controller: 'one'], { 'link text' })
或bean.select(attrs)
.您可以尝试在您的taglib 并返回正确的闭包 - 我不确定。
Invoke other taglibs' tags by their namespace, like
g.link([controller: 'one'], { 'link text' })
, orbean.select(attrs)
.You can try writing
getProperty
in your taglib and return proper closures - I'm not sure.