Fractal layouts using Handlebars.js

I have a simple hierarchy of objects consisting of:

Category String name List childCategories; 

I would like to introduce this layout using rudders in a general way, but it's hard for me to figure out how to deploy layouts. Given this layout:

 <script id="categories-template" type="text/x-handlebars-template"> {{#categories}} <ul > <li> <span>{{name}}</span> <div>{{#childCategories}}{{/childCategories}}</div> </li> </ul> {{/categories}} </script> 

What is the best way to reuse the existing categories template for all child categories? Do I need to register a handler? Is there an easier way?

+8
json javascript templates
source share
2 answers

Two patterns

 <!-- language: html --> <script id="categories-template" type="text/x-handlebars-template"> {{#categories}} <ul > <li> <span>{{name}}</span> {{#if childCategories}} <div>{{&testHelper childCategories}}</div> {{/if}} </li> </ul> {{/categories}} </script> <script id="categories-nested" type="text/html"> {{&testHelper categories}} </script> 

And assistant handlebers

 Handlebars.registerHelper('testHelper', function(info) { var template = Handlebars.compile($('script#categories-template').html()); return template(categories); }); 
+16
source share
 <script id="categories-template" type="text/x-handlebars-template"> <ul> {{#each categories}} <li> <span>{{name}}</span> <div> <ul> {{#each childCategories}} <li><!-- content: blah-blah-blah --></li> {{/each}} </ul> </div> </li> {{/each}} </ul> </script> 
-2
source share

All Articles