nested loops...

0 views
Skip to first unread message

George Moschovitis

unread,
Aug 13, 2010, 7:32:05 AM8/13/10
to Normal-Template
I am copying the answer to a question that may be useful to others.

QUESTION:
...
i wasn't able to figure out how to iterate through a deep hierarchy of
elements. for example, given this data:

var data = {

ul: [
'List Item 1',
'List Item 2',
[
'List Item 3.1',
'List Item 3.2'
]
]
};
or this:
data.ul=[

{value:'List Item 1'},
{value:'List Item 2'},
{values:
[
{value: 'List Item 3.1'},
{value: 'List Item 3.2'}
]
}
];
how can i get this?

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>
<ul>
<li>Item 3.1</li>
<li>Item 3.2</li>
</ul>
</li>
</ul>


ANSWER:

Try this:

var data = {
items: [
{title: "item 1"},
{title: "item 2"},
{title: "item 3", items: [
{title: "item 3.1"},
{title: "item 3.2"}
]},
{title: "item 4"}
]
}

<ul>
{:r items}
<li>
{=title}
{:if children}
<ul>
{:r items}
<li>{=title}</li>
{/:r}
</ul>
{/:if}
</li>
{/:r}
</ul>


-g.
Reply all
Reply to author
Forward
0 new messages