2

我有以下代码块用于写出一系列按钮元素,其中包含图标和文本:

def tagcloud = { attrs, body ->

    def mb = new MarkupBuilder(out)

    mb.ul('class': 'list-inline') {
        def tag = it
        attrs.tags.split(",").each {
            li {
                button('class': 'btn btn-default', 'type': 'submit') {
                    i('class': 'fa fa-tag', '')
                    mb.yield('test')
                }
            }   
        }
    }
}

但是,我发现在使用 yield 函数时,标记生成器将 yield 调用作为我的 html 中的标签而不是原始文本输出:

<li>
    <button class="btn btn-default" type="submit">
      <i class="fa fa-tag"></i>
      <yield>test</yield>
    </button>
</li>

根据我的研究,这是推荐的方法:使用 Groovy MarkupBuilder 的 HTML,我如何优雅地混合标签和文本?

有谁知道为什么这个文本被这样包装?

我正在使用 Grails 2.3.8。

4

1 回答 1

5

Simply this way:

def tagcloud = { attrs, body ->

    def mb = new MarkupBuilder(out)

    mb.ul('class': 'list-inline') {
        def tag = it
        attrs.tags.split(",").each {
            li {
                button('class': 'btn btn-default', 'type': 'submit') {
                    i('class': 'fa fa-tag', '')
                    mkp.yield('test')
                }
            }   
        }
    }
}

mkp is a special namespace used to escape away from the normal building mode of the builder and get access to helper markup methods such as 'yield' and 'yieldUnescaped'. See the javadoc for getMkp() for further details.

于 2014-07-31T16:11:41.800 回答