所以我有这个tagLib;
def trimTo = { attrs, body ->
int length = attrs.int('length')
String text = body()
if (text.size() > length) {
text = text.substring(0, length - 1) + '...'
}
out << text
}
通常我是这样使用的;
<g:trimTo length="50">${fieldValue(bean: fooInstance, field: "textEng")}</g:trimTo>
一切都很好。
但我想在看起来像g:select标记中调用它;
<g:select name="phrases" from="${Foo.list()}" optionValue="${{it.textEng}}" multiple="multiple" optionKey="id" size="25" value="${fooParentInstance?.children*.id}" class="many-to-many"/>
具体来说,我想用它来缩短{it.textEng}的optionValue属性所显示的内容。
我尝试了几种不同的组合,似乎无法找出正确的语法。
在此先感谢您的协助。
*针对拼写进行了编辑
最佳答案
设法弄清楚了这一点,主要是通过反复试验,并花了一点点的阅读和推理。
optionValue="${{g.trimTo([length: 50], it.textEng)}}"
谢谢大家
关于grails - Grails:如何在GSP表达式内调用tagLib?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21708496/