gpt4 book ai didi

scala - 无法将 play.twirl.api.Html 对象与 play 2.3 连接起来

转载 作者:行者123 更新时间:2023-12-02 03:27:39 25 4
gpt4 key购买 nike

我正在尝试将一个相当大的项目从 play framework 2.2 迁移到 2.3。在这个项目中,我们有一些 helper 做这样的事情:

import play.api.templates.Html;
...
private object HtmlHelper {
...
// Given a sequence of Htmls, return a single Html containing them
def html(htmls: Seq[Html]): Html = htmls.foldLeft(Html(""))(_+=_)
}

我已将其转换为:

import play.twirl.api.Html;
...
private object HtmlHelper {
...
// Given a sequence of Htmls, return a single Html containing them
def html(htmls: Seq[Html]): Html = htmls.foldLeft(Html(""))((r,c) => r + c)
}

编译失败,出现以下错误:

Read from stdout: <PATH> type mismatch;
Read from stdout: found : play.twirl.api.Html
Read from stdout: required: String

我一直试图在 2.3 中找到有关此 Html 对象的文档,但没有找到任何东西。据我所知,Html 对象实现了 Appendable,这意味着 + 运算符应该可以工作......我没有时间学习所有的 Scala,这种所谓的“表达性”语法让我很紧张。

如有任何帮助,我们将不胜感激。

最佳答案

Html 上没有定义更多的 += 方法,所以编译器试图让它作为一个 String 工作,这也没有工作。查看updated scaladoc (Html 是一种 Appendable)。

This used to support +=, but no longer is required to.

@todo Change name to reflect not appendable

我想您可以组合 String 值,然后转换回 Html

def html(htmls: Seq[Html]): Html =
Html(htmls.foldLeft("")((r, c) => r + c.toString))

scala> val list = List(Html("<p>1</p>"), Html("<p>2</p>"), Html("<p>3</p>"))
list: List[play.twirl.api.Html] = List(<p>1</p>, <p>2</p>, <p>3</p>)

scala> html(list)
res5: play.twirl.api.Html = <p>1</p><p>2</p><p>3</p>

虽然在 HtmlFormat 中,实际上已经有一个名为 fill 的方法:

def html(htmls: Seq[Html]): Html = HtmlFormat.fill(htmls)

Seq 可能有点挑剔。

关于scala - 无法将 play.twirl.api.Html 对象与 play 2.3 连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29525541/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com