Jsrender: Can't access a variable in {{for}}

Created on 15 Jun 2012  路  3Comments  路  Source: BorisMoore/jsrender

Hi Boris,

I am passing the following to the template
{'items': items, ranked: true}

items is an array of objects

In my template, I have

{{for items}}
{{if (ranked)}}{{:#index}}{{/if}}
{{/for}}

But it seems the variable 'ranked' doesn't do anything.

Most helpful comment

Well within the {{for items}} the data context (current data item) is the item of the items array.
So you are testing for the an item with item.ranked == true. But your ranked property is on the object which has the items property, not on the item. Maybe you want

{{for items}}
   {{if #parent.parent.data.ranked)}}{{:#index}}{{/if}}
{{/for}}

Another approach you can use is to create a template variable which copies the ranked property and makes it accessible to the nested templates via the template context:

{{for items ~isRanked=ranked}}
   {{if ~isRanked)}}{{:#index}}{{/if}}
{{/for}}

Data properties are not automatically exposed in nested contexts. (There is no with (data) {} block in JsRender). But template parameters provides a better way to opt in to exposing variables to nested contexts...

All 3 comments

Well within the {{for items}} the data context (current data item) is the item of the items array.
So you are testing for the an item with item.ranked == true. But your ranked property is on the object which has the items property, not on the item. Maybe you want

{{for items}}
   {{if #parent.parent.data.ranked)}}{{:#index}}{{/if}}
{{/for}}

Another approach you can use is to create a template variable which copies the ranked property and makes it accessible to the nested templates via the template context:

{{for items ~isRanked=ranked}}
   {{if ~isRanked)}}{{:#index}}{{/if}}
{{/for}}

Data properties are not automatically exposed in nested contexts. (There is no with (data) {} block in JsRender). But template parameters provides a better way to opt in to exposing variables to nested contexts...

I have a jquery datatable where I'm displaying child rows using js render. For both the columns on the table the data is coming from 2 different arrays. So how should I break the data into 2 separate rows which is currently displaying as comma separated list

@Krish2215: This is for reporting bugs and other issues, not for general help questions. You could try stackoverflow https://stackoverflow.com/questions/tagged/jsrender. But you will need to give much more specific context, and ideally a jsfiddle to show your exact difficulty....

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ba1dr picture ba1dr  路  7Comments

daslicht picture daslicht  路  14Comments

steadicat picture steadicat  路  24Comments

paulirish picture paulirish  路  132Comments

mathiasbynens picture mathiasbynens  路  11Comments