在 Play Framework 的变量中获取 Scala 模板

Get scala template in a variable in Play Framework

假设视图文件夹中有两个 Scala 模板

  1. file1.scala.html
  2. container.scala.html

现在我想将第一个模板从控制器传递到第二个模板 (container.scala.html)。喜欢:

public class Application extends Controller {
    static Result isItPossible()
    {
        Result theFile=ok(file1.render());
        return ok(container.render(theFile));
    }
}

可能吗?如果是,我该怎么做?

您可以将呈现的模板传递给 container 模板。 container 需要一些 Html 参数:

container.scala.html:

@(content: Html)

<p>Here's my content: @content </p>

从控制器内部:

public class Application extends Controller {
    return ok(container.render(file1.render()));
}

值得一提的是,您不需要在控制器中组合 包装容器 ,因为模板引擎能够使用 Layouts (as described in docs)。在那种情况下,你可以像这样使用它:

container.scala.html

@()(content: Html)
<p>Here's my content: @content </p>

file1.scala.html

@container() {
    <b>this is content of <i>file1</i> template</b>
}

控制器

public class Application extends Controller {
    static Result itIsPossible() {
        return ok(file1.render());
    }
}