Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing templates in handlebars

I am new to JS templating and Handlebars I have a nested JSON structure, often each parent node is a new object / has different structure so recursion is not the solution I think.

My question is Is it possible to call another template from a template in Handlebars?

My background is XSLT

Example:

<script id="entry-template" type="text/x-handlebars-template">
  <div>{{name}}</div>
  .. call template-2
</script>

<script id="template-2" type="text/x-handlebars-template">
  <div>{{name2}}</div>
   .. call template-3
</script>

<script id="template-3" type="text/x-handlebars-template">
  <div>{{name3}}</div>
</script>

.. and so on

Anyone who has some advice?

Best regards, Bob

like image 633
user1499917 Avatar asked Nov 13 '12 20:11

user1499917


1 Answers

That should be pretty easy using partials, and here is a pretty good tutorial.

Essentially though you just need to define the partial

 Handlebars.registerPartial("template-2", $("#template-2").html());

And then utilize it

<script id="entry-template" type="text/x-handlebars-template">
  <div>{{name}}</div>
  {{> template-2}}
</script>
like image 126
Kris Dahl Avatar answered Sep 30 '22 17:09

Kris Dahl