Why should i use Handlbars Templates in BackboneJS application instead of underscore templates?

115 Views Asked by At

I have seen many people using handlebars templates with BackboneJS instead of underscore templates, even underscore is hard dependency of BackboneJS. Can anyone please tell me benefits of doing so?

3

There are 3 best solutions below

0
On BEST ANSWER

Handlebars:

  1. Compatible with mustache templates - what cam be used to use same syntax, or even templates
  2. More readable syntax

Underscore:

  1. No need to load extra library for template
  2. Underscore its not only template engine. It has 80+ useful functions for working with objects, arrays and collections.

On my point of view there are a lot of advantages and disadvantages of using these libs according project specifics.

0
On

It depends on where you are using and what you are needing a template for.

For example, like Evgeniy said, when you want to deal much with collections, such as SORT, or extracting a particular field's value (from a result set), extracting UNIQUE values ... these are the places where you will be better off using Underscore.

Going back to what Evgeniy said, I think it is those 80+ useful functions that make Underscore significant.

I prefer Handlebars for Templates, because I feel that handlebars is more readable and a bit less complex than Underscore when it comes to Template needs. Again, that's my preference.

But bottom-line is, I user Underscore for the Functions it provides and Handlebars for Templates.

Hope this helps.

0
On

Underscore provides the basic functions needed for any project and thats the advantage of that.

At the same time Handlebars can be efficiently used in a project which needs to do lot of formatting/ common functions with some set of data. For e.g. if a project has requirement of displaying a name in a specific format and has to be in sync across the application, then handlebars come in handy.

We can simply write a handlebar and call that function from all the places in the application.

In short i would say both are handy for development and they are wonderful in their own ways.

Hope i have not confused you :)