Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove unnecessary linebreaks on template output?

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!

like image 347
stefan.at.wpf Avatar asked Dec 31 '12 15:12

stefan.at.wpf


2 Answers

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");
like image 74
Marius Soutier Avatar answered Oct 29 '22 17:10

Marius Soutier


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')
like image 33
Kim Stebel Avatar answered Oct 29 '22 15:10

Kim Stebel