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>
Which will get messy with additional @defining
or other statements.
So, is there a way to prettify/beautify Scala templates rendering in order to get rid of extra white spaces?
UPDATE:
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>
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.
Play Framework is an open-source web application framework which follows the model–view–controller (MVC) architectural pattern. It is written in Scala and usable from other programming languages that are compiled to JVM bytecode, e.g. Java.
A Play Scala template is a simple text file that contains small blocks of Scala code. Templates can generate any text-based format, such as HTML, XML or CSV. The template system has been designed to feel comfortable to those used to working with HTML, allowing front-end developers to easily work with the templates.
Razor minimizes the number of characters and keystrokes required when writing a view template, and enables a fast, fluid coding workflow.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With