Ian Davis
Ian Davis

Reputation: 19423

How to render values of an array containing strings or any other basic type using JsRender

Using JsRender, I'm trying to render out the values of an array full of strings, and they're coming out as blank. I've tried {{:$value}}, {{:$data}}, {{:value}} and {{:data}} and none of them work. What is the syntax to do this?

Template:

<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);

It's rendering...

John wrote these books:
book= book= book= 

Sarah wrote these books:
book= book= book=

Live example here: http://jsbin.com/kutude/1/edit?html,js

Upvotes: 1

Views: 1052

Answers (2)

BorisMoore
BorisMoore

Reputation: 8524

You're right, you can write:

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

But alternatively you can simply write:

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

Since {{:xxx}} defaults to the current data item, so you can access it either with no parameter, or with the parameter #data

Upvotes: 2

Ian Davis
Ian Davis

Reputation: 19423

It's {{:#data}} ! Update: {{for books}}<span>{{:#data}}</span>{{/for}}

Upvotes: 0

Related Questions