如何将构建委托给方法?
我正在编写一个 Groovlet,并希望将 HTML 构建器的一部分委托给一个方法,但在使其正常工作时遇到困难。以下是我所拥有的:
def pages = [page1: html.p("page1")]
html.html {
p("p")
pages[page1]
}
我期待以下输出:
<html>
<p>p</p>
<p>page1</p>
</html>
相反,我得到的是以下内容:
<p>text</p>
<html>
<p>p</p>
</html>
我做错了什么?
I am writing a Groovlet and would like to delegate part of the HTML builder to a method but am having trouble getting it to work. Below is what I have:
def pages = [page1: html.p("page1")]
html.html {
p("p")
pages[page1]
}
I am expecting the following output:
<html>
<p>p</p>
<p>page1</p>
</html>
Instead what I get is the following:
<p>text</p>
<html>
<p>p</p>
</html>
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对有问题的构建器不太熟悉,但我希望做类似的事情:
当然,您可以调用任何闭包或
,而不是
'd 方法。pages[page1]
。 &您需要
delegate.with
以便您正在运行的闭包将其方法调用(如p()
)解析为运行它的闭包的委托(即,HtmlBuilder
)。I'm not overly familiar with the builder in question but I'd expect to be doing something like:
Instead of
pages[page1]
, of course, you could call any closure or a.&
'd method.You need the
delegate.with
so that the closure you're running has its method calls (likep()
) resolved to the delegate of the closure running it (that is, theHtmlBuilder
).