播放框架返回部分视图
问题描述:
我正在使用Play Framework,并且使用AJAX,想要将部分视图返回给调用脚本进行呈现。我来自ASP.NET MVC的世界,所以这是一个非常简单的概念,但我在Play中看不到它的位置。播放框架返回部分视图
我愿做一个例子:
main.html中
<html>
<head><title>Test</title></head>
<body>
<h1>Here's my list</h1>
<input type="text" id="new-entry" /><button id="add-new-entry">Add</button>
<ul id="item-list">
#{list items, as:'item'}
<li>#{anitemtemplate item}</li>
#{/list}
</ul>
<script>
$(function() {
$("#add-new-entry").click(function() {
var action = #{jsAction @add(':name') /};
var title = $("#new-entry").val();
$.post(action(title), null, function(data) {
var newData = $(document.createElement("li")).html(data);
$("#item-list").append(newData);
});
});
});
</script>
</body>
</html>
anitemtemplate.html
${item.title} <em>by ${item.author}</em>
Me.java
public static void add(String title) {
//add the item
return render("anitemtemplate", newitem); //how to do this??
}
答
它应该成为:
public static void add(String title) {
//add the item
return render("anitemtemplate.html", item);
}
或
public static void add(String title) {
//add the item
renderArgs.put("item", newitem);
return render("anitemtemplate.html");
}
而且如果anitemtemplate.html是在/ app /视图/标签/,像标签使用假设在你的main.html中,你应该使用渲染(“标签/anitemtemplate.html“) - 渲染的第一个参数是来自/ app/views /目录的模板的相对路径。
而且顺便说一句,
这
#{list items, as:'item'}
<li>#{anitemtemplate item}</li>
#{/list}
应该
#{list items, as:'item'}
<li>#{anitemtemplate item /}</li>
#{/list}
有什么错误?传递给渲染的变量总是具有相同的名称,所以它应该是'render(“anitemtemplate,item)'。 – maartencls 2012-03-01 16:31:00