Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsRender loop a List<string>

Tags:

jsrender

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.

like image 681
Blaise Avatar asked Aug 06 '12 13:08

Blaise


3 Answers

{{#data}}

Didn't work for me.

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

{{>#data}}
like image 106
Jonas Geiregat Avatar answered Nov 11 '22 16:11

Jonas Geiregat


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

like image 35
MrOBrian Avatar answered Nov 11 '22 15:11

MrOBrian


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 >

like image 7
Sergey Avatar answered Nov 11 '22 15:11

Sergey