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

后端 未结 5 2067
半阙折子戏
半阙折子戏 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:36

    Of course there is always some option :), trim the body and set header again so (cause after operations on the String it will be returned as text/plain):

    // instead of
    return ok(index.render("some"));
    
    // use
    return ok(index.render("some").body().trim()).as("text/html; charset=utf-8");
    

    for 'beauty' loops or if's you need to write more compact code

    // instead of
    @for(test <- tests) {
      
  • @test.name
  • } // use @for(test <- tests) {
  • @test.name
  • }

    And finally you can use some compressor (ie. com.googlecode.htmlcompressor) to... minify whole page (in this sample for production mode only)

    String output = index.render("some").body().trim();
    if (Play.isProd()) output = compressor.compress(output);
    return ok(output).as("text/html; charset=utf-8");
    

提交回复
热议问题