Is it possible to prettify scala templates using play framework 2?

后端 未结 5 2072
半阙折子戏
半阙折子戏 2021-01-31 20:10

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:<

5条回答
  •  悲哀的现实
    2021-01-31 20:19

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

    Add HtmlCompressor as a plugin

    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;
        }
    }
    

    Then every controllers should extend PrettyController.

提交回复
热议问题