Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handlebars.js: Use a partial like it was a normal, full template

I have a template that I want to use both as a partial, and by itself from javascript.

like image 868
Jonathan Dumaine Avatar asked Aug 19 '13 04:08

Jonathan Dumaine


People also ask

Where do I register my Handlebars partials?

In order to use a partial, it must be registered via Handlebars. registerPartial . Handlebars. registerPartial('myPartial', '{{prefix}}');

Is Handlebars a template engine?

Handlebars is a simple templating language. It uses a template and an input object to generate HTML or other text formats.

How do you use partials in JavaScript?

Partials are basically functions that return functions with some already predefined arguments and need some arguments to be completed. Let's say you have a function with several arguments to be set, but you don't want to set the majority of arguments over and over again because you need it more than once.


4 Answers

If your templates are precompiled, you can access your partials via Handlebars.partials['partial-name']() as well as call them from a template via the {{> partial}} helper.

This is nice because you can then write a utility function for rendering a template whether it be a full blown template or partial.

ex:

function elementFromTemplate(template, context) {     context = context || {};     var temp = document.createElement('div');     temp.innerHTML = templates[template] ? templates[template](context) : Handlebars.partials[template](context);     return temp.firstChild; } 

myDiv.appendChild(elementFromTemplate('myPartial', context));

myDiv.appendChild(elementFromTemplate('a-whole-template'));

Hope this helps anyone else who wants to use Handlebars like I do.

like image 82
Jonathan Dumaine Avatar answered Sep 28 '22 00:09

Jonathan Dumaine


It's easier to do it the other way around - to compile all your templates as normal templates, then make them available as partials:

Handlebars.partials = Handlebars.templates

This makes it possible to use your templates as usual, and as partials as well:

{{> normalTemplate}}
like image 35
Frederik Wordenskjold Avatar answered Sep 28 '22 02:09

Frederik Wordenskjold


To render a partial from javascript you can use

Handlebars.partials["myPartial"]()
like image 22
Adil Avatar answered Sep 28 '22 01:09

Adil


To use a partial from a template, simply include {{> partialName}}.

<script id="base-template" type="text/x-handlebars-template">
  <div>
    {{> person}}  <!-- This is the partial template name -->
  </div>
</script>

<script id="partial-template" type="text/x-handlebars-template">
  <div class="person">
    <h2>{{first_name}} {{last_name}}</h2>
    <div class="phone">{{phone}}</div>
  </div>
</script>

<script type="text/javascript">
  $(document).ready(function() {
    var template = Handlebars.compile($("#base-template").html());

    //Aliasing the template to "person"
    Handlebars.registerPartial("person", $("#partial-template").html()); 

    template(yourData);
  }
</script>
like image 25
Raoul George Avatar answered Sep 28 '22 02:09

Raoul George