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

查看:86
本文介绍了是否可以使用Play Framework 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答案并完成了以下步骤:

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.

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

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