Skip to content

Instantly share code, notes, and snippets.

@zekiunal
Forked from jrdmcgr/mustache-recursive.html
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save zekiunal/f9c9f01c31b8f3f8a0b2 to your computer and use it in GitHub Desktop.

Select an option

Save zekiunal/f9c9f01c31b8f3f8a0b2 to your computer and use it in GitHub Desktop.
<html>
<body>
<div id="hierarchy"></div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.2/mustache.min.js"></script>
<script type="text/template" id="recursive-partial">
{{#children}}
<li>{{name}}
<ul>
{{>recursive_partial}}
</ul>
</li>
{{/children}}
</script>
<script>
partial = $('#recursive-partial').html()
tmpl = '<ul>{{>recursive_partial}}</ul>';
data = {
children:
[
{
name: 'Foo',
children:
[
{
name: 'Foo.bar',
children: null
},
{
name: 'Foo.baz',
children: null
}
]
},
{
name: 'Bar',
children:
[
{
name: 'Bar.bar',
children: null
},
{
name: 'Bar.baz',
children: null
}
]
}
]
};
html = Mustache.render(tmpl, data, { recursive_partial: partial });
$('#hierarchy').html(html)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment