grails 3 taglib,html 构建器和 createLInk

grails 3 taglib, html builder and createLInk

是否可以在 grails 3 中使用 createLink 和 HTMLBuilder 创建标签库。我试过 :

def buttonTaglib = { attrs, body ->
        def mb = new groovy.xml.MarkupBuilder(out)
        mb.button(type: "button", class: "btn btn-success") {
            g.createLink(controller: 'test', action: 'show', id: 1) { mkp.yield "buttonTaglib" }
        }
    }

def buttonTaglib2 = { attrs, body ->
        def mb = new groovy.xml.MarkupBuilder(out)
        out << g.createLink(controller:"test", action:"show") << mb.button(type: "button", class: "btn btn-success") { "buttonSimple" }
    }

没有成功。

标准使用的典型输出是否足够,如果是这样就这么简单:

def buttonTaglib2 = { attrs, body ->
        out << """
<a href="${g.createLink(controller:"test", action:"show")}" class="btn btn-success">Test</a>
"""
    }

您也可以只渲染一个模板并将参数或属性传递给模板。然后像往常一样从控制器到视图处理您的值。

这个对我有用:

def button = { attrs, body ->
        def mb = new groovy.xml.MarkupBuilder(out)
        out << link(controller: attrs.controller, action: attrs.action, id: attrs.id, params: attrs.params ) {
            mb.button(type: attrs.type, class: attrs.class) {
                mkp.yield attrs.name
            }
        }
}