Loading JSON Data in Scala PlayFramework Templates

The goal is to move from the Scala model / ViewModel to raw JSON, which can be loaded into the view template to avoid requests for JSON data after the page loads.

And an example of what I was playing with but no luck:

@(todos: play.api.libs.json.JsValue) @import play.api.libs.json.Json <html> <head>...</head> <body>...</body> <script> var todos = JSON.parse(' @Json.stringify(todos) '); </script> </html> 

Basically, he spills out a lot of quoted text into action:

 [{&quot;id&quot;:&quot;:&quot;294858e2-c9eb-4f50-9eac-47b257573d83&quot;}] 

No luck with Google or PlayFramework docs, so I would really like the help.

+5
source share
1 answer

The playback template engine will avoid any lines that you render in HTML, which will carefully distort your JSON.

To print it verbatim, execute @Html(Json.stringify(todos)) as described here .

+8
source

Source: https://habr.com/ru/post/1213095/


All Articles