8
votes

A question on the {{for}} loop in jsRender.

The demo shows we can loop through a collection of complex objects and display their properties:

{{for languages}}
    <div>
        <em>{{>name}}</em>
    </div>
{{/for}}

But what if my languages is only a List<string>? There will be no {{>name}} to be displayed. How can we reference the individual string values?

Thanks.

4

4 Answers

19
votes
{{#data}}

Didn't work for me.

Something seems to have been changed, this is what did it for me:

{{>#data}}
11
votes

You should be able to use #data to access the individual string values inside the loop.

6
votes

You should use:

{{>#data}} or {{>}} - (encodes HTML)

{{:#data}} or {{:}} - (non HTML)

For example:

Let's say your languages object looks like this:

var languages = ['en', 'sp', 'zh'];

{{for languages}}
    <div>
        <em>{{>}}</em>
    </div>
{{/for}}

Will result in:

<div>
   <em>en</em>
</div>
<div>
   <em>sp</em>
</div>
<div>
   <em>zh</em>
</div>

Documentation

#data

Difference between : and >

0
votes

Also, if you want to do some comparisons, you can use code like:

{{if #data == 'xxx' || #data == 'yyy'}}
...
{{/if}}

in the script.