PDA

View Full Version : How to use ext template (Tpl) to accomplish this?



d007
20 Sep 2011, 12:55 AM
If I have an array of unknown data, how can I tell extjs to loop through the data and generate a template based on what is inside the data.
So I have an array with values item1, item2, item3,..., item 50
My current template does an if statement for every single possible item like this:


'<tpl for=".">',

'<tpl if="item1">',
'<span>{item1}</span>',
'</tpl>',
'<tpl if="item2">',
'<span>{item2}</span>',
'</tpl>',
etc
Which I am thinking is very inefficient as I have a lot of items to check for. Whereas if I could loop through 'array' which has only the items that were sent to the store from the server it would be better.
How can I accomplish this? Thanks

aacoro
20 Sep 2011, 1:51 AM
If I have an array of unknown data, how can I tell extjs to loop through the data and generate a template based on what is inside the data.
So I have an array with values item1, item2, item3,..., item 50
My current template does an if statement for every single possible item like this:


'<tpl for=".">',

'<tpl if="item1">',
'<span>{item1}</span>',
'</tpl>',
'<tpl if="item2">',
'<span>{item2}</span>',
'</tpl>',
etc
Which I am thinking is very inefficient as I have a lot of items to check for. Whereas if I could loop through 'array' which has only the items that were sent to the store from the server it would be better.
How can I accomplish this? Thanks

Hi,

I think I would do it this way:



'<tpl for="item1">',
'{.}', <Your specific template>
'</tpl>',


If it detects item1 it will use that part.. instead of everyting..
With a . (dot) it is used by everything, see it as the base:



'<tpl for=".">',
'<div style="background-color: {color}; margin: 3px;">', // general style for this block
// previous piece will be place here
'</tpl>',

d007
20 Sep 2011, 3:08 AM
Hi thanks for the reply but I think you are misunderstanding the question.

Your solution simply swaps an if statement for each item with a for statement for every item. I still have to do that 'for' with every potential item.

mitchellsimoens
20 Sep 2011, 4:49 AM
@d007 Please do not double post. You posted the same thing yesterday in the Discussion forum.