Using Play 2 I am realising a simple REST API, the output is plain text. My template looks like this:
@(items: Map[String,String])
@for((key, value) <- items) {
@value
@key
}
In the controller:
return ok(views.html.bla.render(itemsMap)).as("text/plain");
This gives the following output:
(empty line)
(empty line)
value
key
(empty line)
value
key
I want to get rid of the first 2 empty lines - is that possible?
Putting the for in the first line removes one of the empty lines at the top, however one still remains and for in the first line makes the template hard to read ): Thanks for any hint!
First off, if you use plain text, you should use txt templates (bla.scala.txt
). They also automatically set text/plain; charset=utf-8
content type.
To trim the content, you can return the rendered content directly:
return ok(views.txt.bla.render(itemsMap).body().trim());
In case you want to render HTML content you'd need to change this manually:
return ok(views.html.ble.render().body().trim()).as("text/html; charset=utf-8");
If you are generating plain text output from a map, why do you use views at all? They don't provide any benefit in your case.
You can write the render function in pure Scala. Something like
items.map{ case (k,v) => v + '\n' + k}.mkString('\n')
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