将 JSON 数据引导至 Scala PlayFramework 模板
Bootstrapping JSON data into Scala PlayFramework templates
目标是从用 Scala 编写的 Model/ViewModel 到可以引导到视图模板中的原始 JSON,以避免在 JSON 之后请求 JSON 数据页面加载。
以及我一直在玩但运气不佳的示例:
@(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>
基本上它会吐出很多引用的文字,效果如下:
[{"id":":"294858e2-c9eb-4f50-9eac-47b257573d83"}]
在 Google 或 PlayFramework 文档方面运气不佳,所以我希望得到一些帮助。
Play 模板引擎会将您呈现的任何字符串转义为 HTML,这将彻底破坏您的 JSON。
要逐字输出,请执行 @Html(Json.stringify(todos))
,如 here 所述。
目标是从用 Scala 编写的 Model/ViewModel 到可以引导到视图模板中的原始 JSON,以避免在 JSON 之后请求 JSON 数据页面加载。
以及我一直在玩但运气不佳的示例:
@(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>
基本上它会吐出很多引用的文字,效果如下:
[{"id":":"294858e2-c9eb-4f50-9eac-47b257573d83"}]
在 Google 或 PlayFramework 文档方面运气不佳,所以我希望得到一些帮助。
Play 模板引擎会将您呈现的任何字符串转义为 HTML,这将彻底破坏您的 JSON。
要逐字输出,请执行 @Html(Json.stringify(todos))
,如 here 所述。