Grails:从 g:if 标签调用 taglib [英] Grails: Call taglib from g:if tag
问题描述
我有一个自定义标签库,它返回一个布尔对象,以便我的 GSP 可以决定是否显示一段 html.我想使用 g:if 标签来检查这个布尔值的值,因为我还需要检查一些其他值(在 taglib 中是不可访问的).但是,我不知道如何从标签中实际调用 taglib?
I have a custom tag lib that returns a Boolean object so that my GSP can decide whether to display a piece of html or not. I would like to use the g:if tag to check this Boolean's value since I also need to check a few other values (that aren't accesible in the taglib). However, I don't know how to actually call the taglib from the tag?
我试过了:
<g:if test="${<custom:tag/> && other.boolean}">
但这会引发错误.
我也试过:
<g:if test="<custom:tag/> && ${other.boolean}">
但这也会引发错误.
推荐答案
taglib 是什么样子的?看看它的用法应该如下:
How does the taglib look like? Looking at the usage it should be as below:
class SomeTagLib {
static namespace = "custom"
static returnObjectForTags = ['tag']
def tag = { attrs, body ->
//returns an object (can be boolean)
}
}
默认情况下,标签库会返回 流字符缓冲区.如果您需要返回一个对象(如在您的情况下用作条件语句的一部分),我想您需要 returnObjectFromTags
如上所示.它指定哪个标签偏离默认行为并返回一个对象.
By default, taglibs would return StreamCharBuffer. If you need an object to be returned, (as in your case to be used as part of conditional statement), I guess you would need returnObjectFromTags
as shown above. It specifies which tag deviates from default behavior and returns an object instead.
此外,您应该使用 Tim 提到的 taglib:
Also, you should be using the taglib as mentioned by Tim:
<g:if test="${custom.tag() && other.boolean}"> //should be the appropriate way
这篇关于Grails:从 g:if 标签调用 taglib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!