渲染Play框架2.0模板的一部分

我正在尝试从控制器调用渲染中的标签(函数)而不是模板。 这样我可以使用它来从ajax调用中对页面进行部分渲染。 当然,我可以在几个模板中分离表单的组件,并在那些上调用渲染,但我认为它会更清洁。

我试图做的是如下:

formpage.scala.htm

@()  ... @content ...  @********************************** * Helper generating form * ***********************************@ @content() = { 

@Messages("employees")

@form(routes.AppController.save()) { @inputText... ... }

并使用ajax渲染内容函数,而不必将其分离到单独的文件中。 这样我就可以渲染模板的一部分而不会在多个文件中将其分段。

事实上,标签只是较小的模板,因此您可以使用标签 – 在模板和控制器中使用最简单的示例:

/app/views/tags/mytag.scala.html

 This is my tag... 

在控制器中可以呈现为:

 public static Result createFromTag(){ return ok(views.html.tags.mytag.render()); } 

在其他模板中,您只需要插入:

 .... And there is my tag rendered @tags.mytag() 

更灵活

当然,因为它是模板ergo Scala函数,你可以将一些参数传递给它甚至是Html体:

/app/views/tags/othertag.scala.html

 @(headline: String)(body: Html) 

@headline

@body

在控制器中可以呈现为:

 public static Result createFromTag(){ return ok( views.html.tags.othertag.render( "Head from controller", new play.api.templates.Html("This code becomes from controller") ) ); } 

(当然你可以在动作import play.api.templates.Html;导入这两个更短的代码import play.api.templates.Html;import views.html.tags.othertag

最后,在您的模板中,您可以将标记用作:

 And there is my tag rendered 
@tags.othertag("Head from template"){ some content for the tag's body from The Template! }

最后。

您可以在文档中找到标签说明。