Created
June 19, 2013 18:28
-
-
Save jrdmcgr/5816645 to your computer and use it in GitHub Desktop.
Recursive partial in mustache.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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
Did you ever find a solution to the overflow problem/bug here?