渲染Play框架2.0模板的一部分 [英] Rendering a portion of a Play framework 2.0 template

查看:99
本文介绍了渲染Play框架2.0模板的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to call render from a controller for a tag (function) inside a template instead of the template. This way I could use it for partial renderings of a page from ajax calls. Of course I could separate the components of the form in several templates an call render on those but I think it would be cleaner the other way.

我想要做什么如下所示:

What I was trying to do is like the following:

formpage.scala.htm

formpage.scala.htm

@()
<html>
...

@content

...
</html>


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

<h3 class="form-heading">@Messages("employees")</h3>

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

@inputText...
...

}

使用ajax渲染内容函数,而不必将其与
单独文件分开。通过这种方式,我可以渲染模板的一部分而不会在多个文件中将其分割为

And using ajax render the content function, without having to separate it to a separate file. This way I could render portions of the template without fragmenting it in multiple files.

推荐答案

事实上标签是只是较小的模板,所以你可以使用标签 - 在模板和控制器中使用最简单的样本:

De facto the tag is just smaller template, so you can use tags for both - using in templates and controllers, the simplest sample:

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

This is my tag...

在控制器中可以呈现为:

In controller can be rendered as:

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

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

In other template you just to insert:

....
And there is my tag rendered
<b>@tags.mytag()</b>






更灵活



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


More flexibility

Of course as it's template ergo Scala function as well you can just pass some params to it or even Html body:

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

@(headline: String)(body: Html)

<h3>@headline</h3>
<div class="tagsBody">
    @body
</div>

在控制器中可以呈现为:

In controller can be rendered as:

public static Result createFromTag(){

    return ok(
            views.html.tags.othertag.render(
                    "Head from controller",
                    new play.api.templates.Html("This code becomes from <i>controller</b>")
            )
    );
}

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

(of course you can import these two for shorter code in action import play.api.templates.Html; and import views.html.tags.othertag)

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

Finally in your template you can use the tag as:

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

final。

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

You'll find tags description in documentation.

这篇关于渲染Play框架2.0模板的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆