javascript - Playframework 2.x (Scala) : Passing a Twirl/HTML code from a controller's function to the view

标签 javascript playframework twirl

我的目标:了解我可以从 Controller 发送以查看的参数的限制(如果有)。现在,我正在检查是否可以将任何 Twirl 的关键字(例如,@、@for 等)从 Controller 函数发送到 View ,然后使用 JS 重新加载页面的一部分。

我们可以在使用as(HTML)时将原始HTML传递给 View 。在 Controller 功能中。但是是否可以发送 scala.html 的 block ?输入代码及其所有语法糖,从 Controller 的函数到 View 相关的 JS 函数;这样它就可以在相关的div中显示它?例如,我如何/可以传递以下内容:

<p style="padding:1rem;"> @for(i <- items){<h2>i.name</h2} </p>

如果我将以上内容包装在 Controller 函数中:

Ok(<p style="padding:1rem;"> @for(i <- items){<h2>i.name</h2} </p>).as(HTML)

没错,我会收到以下错误;因为代码不是纯 HTML:

enter image description here

那有办法吗?或者所有模板相关引擎的关键字都无法从 Controller 发送到 View ?并且只需要在 View 的文件上使用。

最佳答案

twirl 代码在服务器上编译为常规 scala 函数(请参阅 https://www.playframework.com/documentation/2.5.x/ScalaTemplateUseCases#Tags-%28they-are-just-functions,-right?%29 )

您可以创建一个像 renderItems.scala.html 这样的文件

@(items: List[Item])
<p style="padding:1rem;"> @for(i <- items){<h2>@i.name</h2} </p>

并这样调用它

Ok(renderItems(items))

关于javascript - Playframework 2.x (Scala) : Passing a Twirl/HTML code from a controller's function to the view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36255896/

相关文章:

javascript - SignalR Javascript 客户端回调未被触发

javascript - 将 jQuery 添加到 Header.php 文件失败

scala - Play Framework 2.5 scala i18n

scala - 如何使用 akka http 将 html、css 和 js 合二为一?

javascript - 可以将模型传播到组件,但不能传播到 sap.ui.jsfragment

javascript - StreetView 中标记上的 InfoWindows

twitter - 如何配置http ://localhost:9000 to http:/mylocal. loc

google-app-engine - 确定 Google 应用引擎运行缓慢的原因

java - play框架1.3.3和hibernate缓存