是否可以使用播放框架 2 美化 Scala 模板? [英] Is it possible to prettify scala templates using play framework 2?

查看:19
本文介绍了是否可以使用播放框架 2 美化 Scala 模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Play Framework 2 我注意到渲染的 Scala HTML 模板不喜欢缩进的 @if@for.

Using Play Framework 2 I've noticed the rendered Scala HTML templates don't like indented @if or @for.

例如,类似这样的事情:

So, for example, something like that:

<ul>
   @for(test <- tests) {
      <li>@test.name</li>
   }
</ul>

会有多余的不需要的空间.要修复它,我需要做这样的事情:

Will have extra unneeded spaces. To fix it, I need to do something like that:

<ul>
@for(test <- tests) {
   <li>@test.name</li>
}
</ul>

附加的 @defining 或其他语句会变得混乱.

Which will get messy with additional @defining or other statements.

那么,有没有一种方法可以美化/美化 Scala 模板渲染以去除多余的空白?

So, is there a way to prettify/beautify Scala templates rendering in order to get rid of extra white spaces?

更新:

阅读这个主题我注意到由于模板顶部的参数,还添加了额外的空格和换行符.所以这个:

Reading this thread I've noticed extra spaces and line breaks are added as well because of the parameters on top of the templates. So this:

@(myParam: String)


<!DOCTYPE html>
<html>
   <head></head>
   <body></body>
</html>

将在生成的 HTML 之上添加 3 个额外的换行符.这肯定很烦人.

will add 3 extra line breaks on top of the resulting HTML. Which is definitely annoying.

该线程似乎说目前没有更正的选项.

The thread seems to say there are no option at the moment to correct that.

推荐答案

因此,有关更多详细信息,我使用了 @biesor answer 并完成了以下步骤:

So for more details I've used @biesor answer and went through these steps:

添加 HtmlCompressor 作为插件

在 Build.scala 中:

In Build.scala:

val appDependencies = Seq(
    "com.googlecode.htmlcompressor" % "htmlcompressor" % "1.5.2"
)

PrettyController

public class PrettyController extends Controller {

    public static Results.Status ok(Content content) {
        return Results.ok(prettify(content)).as("text/html; charset=utf-8");        
    }

    public static Results.Status badRequest(Content content) {
        return Results.badRequest(prettify(content)).as("text/html; charset=utf-8");        
    }

    public static Results.Status notFound(Content content) {
        return Results.notFound(prettify(content)).as("text/html; charset=utf-8");      
    }

    public static Results.Status forbidden(Content content) {
        return Results.forbidden(prettify(content)).as("text/html; charset=utf-8");     
    }

    public static Results.Status internalServerError(Content content) {
        return Results.internalServerError(prettify(content)).as("text/html; charset=utf-8");       
    }

    public static Results.Status unauthorized(Content content) {
        return Results.unauthorized(prettify(content)).as("text/html; charset=utf-8");      
    }

    private static String prettify(Content content) {
        HtmlCompressor compressor = new HtmlCompressor();
        String output = content.body().trim();

        if (Play.isDev()) {
            compressor.setPreserveLineBreaks(true);
        }

        output = compressor.compress(output);

        return output;
    }
}

那么每个控制器都应该扩展PrettyController.

Then every controllers should extend PrettyController.

这篇关于是否可以使用播放框架 2 美化 Scala 模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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