- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标:了解我可以从 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:
那有办法吗?或者所有模板相关引擎的关键字都无法从 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/
我是一名优秀的程序员,十分优秀!