When using Groovy MarkupBuilder
, I have places where I need to output text into the document, or call a function which outputs text into the document. Currently, I'm using the undefined tag "text" to do the output. Is there a better way to write this code?
使用Groovy MarkupBuilder时,我需要将文本输出到文档中,或者调用将文本输出到文档中的函数。目前,我正在使用未定义的标签“text”来进行输出。有没有更好的方法来编写这段代码?
li {
text("${type.getAlias()} blah blah ")
function1(type.getXYZ())
if (type instanceof Class1) {
text(" implements ")
ft.getList().each {
if (it == '') return
text(it)
if (!function2(type, it)) text(", ")
}
}
}
2 个解决方案
#1
8
Actually, the recommended way now is to use mkp.yield
, e.g.,
实际上,现在推荐的方法是使用mkp.yield,例如,
src.p {
mkp.yield 'Some element that has a '
strong 'child element'
mkp.yield ' which seems pretty basic.'
}
to produce
<p>Some element that has a <strong>child element</strong> which seems pretty basic.</p>
#2
2
Include a method:
包括一个方法:
void text(n){
builder.yield n
}
Most likely you (I) copied this code from somewhere that had a text method, but you didn't also copy the text method. Since MarkupBuilder accepts any name for the name of a tag and browsers ignore unknown markup, it just happened to work.
很可能你(我)从有文本方法的地方复制了这段代码,但是你也没有复制文本方法。由于MarkupBuilder接受标签名称的任何名称,并且浏览器忽略未知标记,因此它恰好起作用。
#1
8
Actually, the recommended way now is to use mkp.yield
, e.g.,
实际上,现在推荐的方法是使用mkp.yield,例如,
src.p {
mkp.yield 'Some element that has a '
strong 'child element'
mkp.yield ' which seems pretty basic.'
}
to produce
<p>Some element that has a <strong>child element</strong> which seems pretty basic.</p>
#2
2
Include a method:
包括一个方法:
void text(n){
builder.yield n
}
Most likely you (I) copied this code from somewhere that had a text method, but you didn't also copy the text method. Since MarkupBuilder accepts any name for the name of a tag and browsers ignore unknown markup, it just happened to work.
很可能你(我)从有文本方法的地方复制了这段代码,但是你也没有复制文本方法。由于MarkupBuilder接受标签名称的任何名称,并且浏览器忽略未知标记,因此它恰好起作用。