Groovy 标记生成器标签

发布于 2024-09-06 03:50:30 字数 457 浏览 8 评论 0原文

有没有办法修改之前答案中的代码

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

def awaiting = ['one', 'two', 'three']

builder.html {
    ul {
        awaiting.each { 
            li(it.toString())
        }
    }
}

println writer.toString()

,以便添加标签而不是发送它 - 例如

    builder.html{
tag{
    awaiting.each{}
    }
} return result

可以是“ol”或“ul”

Is there a way to modify the code from a previous answer

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

def awaiting = ['one', 'two', 'three']

builder.html {
    ul {
        awaiting.each { 
            li(it.toString())
        }
    }
}

println writer.toString()

so that if you add a tag instead that you send it - like

    builder.html{
tag{
    awaiting.each{}
    }
} return result

could be 'ol' or 'ul' for example

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

书间行客 2024-09-13 03:50:31

您可以依赖 GString,并且可以通过其字符串值调用函数。

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

def awaiting = ['one', 'two', 'three']
def tag = 'ol'

builder.html {
    "$tag" {
        awaiting.each { 
            li(it.toString())
        }
    }
}

println writer.toString()

You can rely on the GStrings and the fact that you can call a function by its string value.

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

def awaiting = ['one', 'two', 'three']
def tag = 'ol'

builder.html {
    "$tag" {
        awaiting.each { 
            li(it.toString())
        }
    }
}

println writer.toString()
倾城°AllureLove 2024-09-13 03:50:31

您还可以使用 invokeMethod,它比上面的更明确,尽管有点长。

builder.html {
  invokeMethod(tag) {
    awaiting.each { 
      li it
} } }

You could also use invokeMethod which is more explicit then the above, albeit a little bit longer.

builder.html {
  invokeMethod(tag) {
    awaiting.each { 
      li it
} } }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文