将表达式的多行输出缩进到模板的其余部分

Indent a multiline output from an expression to the rest of the template

每当我在 scala 模板中有一个输出多行的表达式时,它会使生成的文档有点混乱,因为它没有正确缩进。

有什么解决办法吗?最佳做法是什么?

您可以做的第一件事是在模板的 Scala 部分(即循环)中尝试一些意图,如果它让您烦恼的话,无论如何这很可能是浪费时间。实际上 HTML 的 "ugly" 格式对页面的工作没有任何副作用,因为每个浏览器都可以很好地处理代码,但是如果它是 正确 或没有。

如果你还没有被定罪,你可以使用一些外部库,即 HtmlCompressor(最适合最小化付费计划的传输率),例如:

public Result compressor(){
    String html = ugly.render("hi there").toString();
    HtmlCompressor compressor = new HtmlCompressor();
    return ok(compressor.compress(html)).as("text/html; charset=utf-8");
}

Jsoup(顺便说一句:它不仅仅是 代码清理器

public Result jsoup(){
    String html = ugly.render("hi there").toString();
    Document doc = Jsoup.parse(html);
    return ok(doc.outerHtml()).as("text/html; charset=utf-8");
}

当然有 raw 示例,为了更容易工作,您可以覆盖 ok(...) 控制器内的操作。

此外,您将为此付出一些 性能 的代价,因此请考虑使用缓存 where/if 可能。

为 Play 2.4 提供的样本 / Java