Grails:从g:if标签中调用taglib [英] Grails: Call taglib from g:if tag

查看:133
本文介绍了Grails:从g:if标签中调用taglib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义标签库,它返回一个布尔对象,以便我的GSP可以决定是否显示一段html。我想用g:if标签来检查这个布尔值,因为我还需要检查其他一些值(在taglib中不可访问)。然而,我不知道如何从标签实际调用taglib?



我试过了:

 < g:if test =$ {< custom:tag />&& other.boolean}> 

但会引发错误。

我还试过:

 < g:if test =< custom:tag />&& $ {other .boolean}> 

但也会引发错误。

解决方案

taglib的外观如何?请看下面的用法:

  class SomeTagLib {
static namespace =custom
static int returnObjectForTags = ['tag']

def tag = {attrs,body - >
//返回一个对象(可以是布尔值)
}
}

默认情况下,taglibs会返回 StreamCharBuffer 的。如果你需要返回一个对象(在你的情况下用作条件语句的一部分),我想你需要 returnObjectFromTags ,如上所示。它指定哪个标签与默认行为有偏差,并返回一个对象。



另外,您应该使用Tim提到的taglib:

 < g:if test =$ {custom.tag()& other.boolean}> //应该是适当的方式


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?

I've tried:

<g:if test="${<custom:tag/> && other.boolean}">

but that throws errors.

I also tried:

<g:if test="<custom:tag/> && ${other.boolean}">

but that throws errors too.

解决方案

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)
    }
}

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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆