[2.2-Scala] CSV template

446 views
Skip to first unread message

Deenar Toraskar

unread,
Nov 28, 2013, 12:57:54 PM11/28/13
to play-fr...@googlegroups.com
Hi

I am trying to create a CSV template in a play 2.2 application, but cannot find any documentation or samples using a CSV template. I figured out that creating a file xxx.scala.txt generates a xxx.template.scala class in the views/txt package, but any more documentation of samples would be appreciated.

Thiago Lima

unread,
Nov 28, 2013, 8:08:08 PM11/28/13
to play-fr...@googlegroups.com
Any template will be processed and output plain text. What this "text" means is up to you. It can be a HTML page, a XML, a properties file or even a CSV.

You could write a scala template as simple as

@(items: Seq[MyItemModel])
My Field A; My Field B; My Field C; My Field D
@for(item <- items) {@item.fieldA;@item.fieldB;@item.fieldC;@item.fieldD
}

That would output, for instance,

My Field A; My Field B; My Field C; My Field D
ValueA1;ValueB1;ValueC1;ValueD1
ValueA2;ValueB2;ValueC2;ValueD2
ValueA3;ValueB3;ValueC3;ValueD3
ValueA4;ValueB4;ValueC4;ValueD4

bryan hunt

unread,
Dec 2, 2013, 4:28:57 AM12/2/13
to play-fr...@googlegroups.com
I would strongly recommend against generating CSV in a template. What happens when they want it to generate comma separated values, or quote everything except numbers, or, or, or...

I've used OpenCsv in the past, as have several of my friends, and we have found it very nice to use, it also works in the other direction, converting CSV into Map and so forth.

http://opencsv.sourceforge.net/

Deenar Toraskar

unread,
Dec 3, 2013, 12:08:32 PM12/3/13
to play-fr...@googlegroups.com
Many thanks for both the responses. I had overlooked the quoting part. I already use opencsv for parsing incoming CSV files, so will go down that route for returning results as well.

Thiago Lima

unread,
Dec 3, 2013, 10:19:58 PM12/3/13
to play-fr...@googlegroups.com
I'll give opencsv a try too, if I ever need to handle CSV files. Thanks for the suggestion, Bryan.


--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/ZyFH5_-nhOU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Thiago Lima
Founder @ Beagle Tech
+55 81 98333821
Reply all
Reply to author
Forward
0 new messages