使用Groovy MarkupBuilder的HTML,我如何优雅地混合标签和文本? [英] HTML using Groovy MarkupBuilder, how do I elegantly mix tags and text?

查看:378
本文介绍了使用Groovy MarkupBuilder的HTML,我如何优雅地混合标签和文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Groovy MarkupBuilder 时,我有地方需要将文本输出到文档中,或者调用一个将文本输出到文档中的函数。目前,我正在使用未定义的标签text来完成输出。有没有更好的方式来写这段代码?

  li {
text($ {type.getAlias() })}
函数1(type.getXYZ())
if(类型instanceof Class1){
text(implements)
ft.getList()。each {
if(it =='')return
text(it)
if(!function2(type,it))text(,)
}
}其实,推荐的方式现在是使用 mkp.yield ,例如

  src.p {
mkp.yield'某元素有一个'
强'子元素'
mkp.yield',这看起来很基本。'
}


$ b

p>某个元素具有< strong>子元素< / strong>这看起来很基本。< / p>


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?

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(", ")
    }
  }
}

解决方案

Actually, the recommended way now is to use mkp.yield, e.g.,

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>

这篇关于使用Groovy MarkupBuilder的HTML,我如何优雅地混合标签和文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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