flatiron / plates

Light-weight, logic-less, DSL-free, templates for all javascript environments!
MIT License
831 stars 69 forks source link

Collection iteration #112

Open pro100sanya opened 10 years ago

pro100sanya commented 10 years ago

Hello, could you give full example for noob, how to iterate over collection?

With html template and data and binding. I am trying to bind values, but cannot get it work.

Thanks.

davidamitchell commented 10 years ago

have a look at the below:

var plates = require('plates');

var markup = '<div data-bind="id"></div><div data-bind="name"></div>';
var data = [
    {
        id:'1',
        name:'bob'
    },
    {
        id:'2',
        name:'sue'
    },
    {
        id:'3',
        name:'sam'
    },
    {   
        id:'4',
        name:'joe'
    }
]

console.log(plates.bind(markup, data));

the output will be:

<div data-bind="id">1</div>
<div data-bind="name">bob</div>
<div data-bind="id">2</div>
<div data-bind="name">sue</div>
<div data-bind="id">3</div>
<div data-bind="name">sam</div>
<div data-bind="id">4</div>
<div data-bind="name">joe</div>

see also https://github.com/flatiron/plates/tree/master/test specifically

let me know if you have any questions.