Scala Play - How to convert a Scala List of Strings to an array of javascript strings (excluding the "problem")?

When converting a Scala string list of strings to a javascript string array with a playback template engine, you probably start with something like this ...

var strArray = [@scalaListOfStrings.mkString(",")]; 

... and finds out that this does not work because there are no quotes around the lines. Then you can try something like this ...

 var strArray = [@scalaListOfStrings.map(s => "\"" + s + "\"").mkString(",")]; 

... just to find out that it will wrap the lines in " and not. " The only way I was able to do this work was ...

 var strArray = [@Html(scalaListOfStrings.map(s => "\"" + s + "\"").mkString(","))]; 

... and my question is: is this the best / only way to do this?

+7
source share
2 answers

You can rely on the Json.toJson () method to do the conversion

 @import play.api.libs.json._ var strArray = @Json.stringify(Json.toJson(List("hello", "world", "everybody"))) 
+5
source

Do not forget @Html.

 @Html(Json.stringtify(Json.toJson(Scala object))) 
+2
source

All Articles