Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jrdmcgr
Created June 19, 2013 18:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jrdmcgr/5816645 to your computer and use it in GitHub Desktop.
Save jrdmcgr/5816645 to your computer and use it in GitHub Desktop.
Recursive partial in mustache.js
<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>
@spencerwhyte
Copy link

Did you ever find a solution to the overflow problem/bug here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment