BorisMoore / jsrender

A lightweight, powerful and highly extensible templating engine. In the browser or on Node.js, with or without jQuery.
http://www.jsviews.com
MIT License
2.67k stars 339 forks source link

Index of first array element #343

Closed paulvales closed 5 years ago

paulvales commented 5 years ago

Hi! When index of array started not with 0 then template dont render. works: http://joxi.ru/krDqVQXsEQ1QPm dont work: http://joxi.ru/DrlzYW4tvYM9b2

BorisMoore commented 5 years ago

Can you show the code you used to create an array with the index not starting with 0? Better, can you create a code sample using JsRender which shows what you are doing? The screenshot of the array is not enough...

paulvales commented 5 years ago

I create array on the backend Yii2 (replaced index array with id item of row in db), i think problem with missing length attribute in array

BorisMoore commented 5 years ago

JsRender works in the client with javascript objects and arrays. A javascript array, such as [a, b, c] starts with index 0, and has a length attribute. Otherwise it is not an array.

Whatever you have on the backend, it must lead to specific javascript objects and arrays in the browser. This is not a valid JsRender issue unless you can provide a simple javascript repro of your problem...

paulvales commented 5 years ago

yes, I got it, Thanks!

BorisMoore commented 5 years ago

Just curious - Are you using JsRender templates on the server on yii2? If so, how? Did you use a yii2 extension or asset that integrates JsRender?

paulvales commented 5 years ago

Backend on the Yii2, return JSON array, on frontend used JsRender for render this JSON array. When i reindex array, on backend, index keys replaced id items from db

BorisMoore commented 5 years ago

http://joxi.ru/krDqVQXsEQ1QPm - this is returning an array [{id=1, ....}, {id=2, ....}, ...] http://joxi.ru/DrlzYW4tvYM9b2 - this is returning an object (a hash) {1: {id=1, ....}, 2: {id=2, ....}, ...}

You can render using {{props items}} to iterate over the keys of an items object (hash) You can render using {{for items}} to iterate over the keys of an items array.

See https://www.jsviews.com/#propstag@iterate-hash.

paulvales commented 5 years ago

"props" helped, thank you very much!

BorisMoore commented 5 years ago

Good to know...