如何使用JsRender呈现包含字符串或任何其他基本类型的数组的值

时间:2014-07-17 19:13:53

标签: json templates jsrender

使用JsRender,我试图渲染出一个充满字符串的数组的值,并且它们会显示为空白。我已尝试{{:$ value}},{{:$ data}},{{:value}}和{{:data}},但这些都无效。这样做的语法是什么?

模板:

<script id="tmpl" type="text/x-jsrender">
  {{:author}} wrote these books:<br/>
  {{for books}}
    book=<span>{{:$data}}{{:$value}}{{:data}}{{:value}}</span>
  {{/for}}
  <br/><br/>
</script>

JS:

var data = [
  { author: "John", books: [ "j1","j2","j3" ]},
  { author: "Sarah", books: [ "s1","s2","s3" ]}
];

var tmpl = $.templates("#tmpl");
var html = tmpl.render(data);
$("div").html(html);

它的呈现......

John wrote these books:
book= book= book= 

Sarah wrote these books:
book= book= book=

此处的实例:http://jsbin.com/kutude/1/edit?html,js

2 个答案:

答案 0 :(得分:2)

你是对的,你可以写:

{{for books}}<span>{{:#data}}</span>{{/for}}

但也可以简单地写一下:

{{for books}}<span>{{:}}</span>{{/for}}

由于{{:xxx}}默认为当前数据项,因此您可以使用无参数或使用参数#data

访问它

答案 1 :(得分:0)

这是{{:#data}}!更新:{{for books}}<span>{{:#data}}</span>{{/for}}