gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:19:49 24 4
gpt4 key购买 nike

我的目标:了解我可以从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com