Orange
Orange

Reputation: 5

How to iterate a <tpl for="."> for a specified number of times?

I need the template to be iterated for only two times even if there are more objects in the array supplied. Please help. Thanks!

Upvotes: 0

Views: 1816

Answers (2)

Selmaril
Selmaril

Reputation: 766

The loop have variable with current index of item at array - xindex. So, you can use inside of loop this index, at condition. To output of first two items of list do check that the xindex is less then 3.

    var data = {
        items: ['first', 'second', 'third', 'the last'],
        title: "The list of first 2 items: "
    };

    var tpl = new Ext.XTemplate('{title}'+
                                '<ol><tpl for="items">'+
                                '<tpl if="xindex &lt; 3">'+
                                '<li>{.}</li>'+
                                '</tpl>'+
                                '</tpl></ol>');
    tpl.append(Ext.getBody(), data);     

Upvotes: 0

Evan Trimboli
Evan Trimboli

Reputation: 30092

There's no way to do a count based iteration. Instead, just create a copy of the array in the data:

var o = {
    items: [1, 2, 3, 4],
    foo: 1
};
var tpl = new Ext.XTemplate('{foo}<tpl for="items">{.}</tpl>');
tpl.apply(Ext.apply(o, {
    items: o.items.slice(0, 2) 
}));

Upvotes: 1

Related Questions