JsRender loop a List <string>

Question about the {{for}} jsRender in jsRender .

The demonstration shows that we can execute a cycle of complex objects and display their properties:

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

But what if my languages are just List<string> ? {{>name}} will not be displayed. How can we refer to individual row values?

Thanks.

+6
source share
4 answers

You can use #data to access individual string values โ€‹โ€‹inside a loop.

+11
source
 {{#data}} 

Not working for me.

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

 {{>#data}} 
+18
source

You should use:

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

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

For instance:

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

The difference between : and >

+2
source

In addition, if you want to make some comparisons, you can use code, for example:

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

in the script.

0
source

Source: https://habr.com/ru/post/922219/


All Articles