I am using backbone.js and underscore.js to build an javascript application. Since hours of reading and trying to run a template within a template like below, it is getting more and more frustrating.
My template using the build in underscore.js template engine:
<script id="navigation_template" type="text/template">
<div><%= title %>
<% _.each(children, function(child) { %>
<% render_this_template_recursively(child) %>
<% }); %>
</div>
</script>
I would like to render this template for every child element ( render_this_template_recursively(child) ).
How can I do this?
Thanks
I’ve not personally tried this but _.template returns a function (I’ve named it templateFn to emphasize that), so you could pass it into the template like this:
Notice that i’m passing in the whole model (assuming that your model has a children property which is itself a collection of backbone models) and your template would be changed to:
Good luck. I hope this works for you