Question

I'm learning backbone.js for a Rails 3 application I'm working on. Backbone uses underscore which, I believe, has its own template engine built in.

I've read good things about mustache but was wondering if I should consider using it instead of the built in template engine of underscore?

What are your thoughts?

Thanks

Was it helpful?

Solution

I am about halfway through my first enterprise level backbone app. I am currently using underscores built in templates because when I started the way I had learned was with underscore.. I don't necessarily have any problem with them. All of the templating solutions are pretty straight forward.

I have since looked at a few of the other solutions and am contemplating switching, but only because I think some of the other solutions look cleaner. Also some of the solutions have a tad bit more functionality.

I like mustache because of its shorter syntax. Looks cleaner. But I if I do switch I think I'm going to go with Handlebars.js.

Handlebars has the same short syntax, plus a ton of other features such as custom helper methods and setting object context inside your template. Check it out here. If I had known about handlebars at the beginning of my project I probably would have jumped on it for sure.

I wouldn't worry so much about adding another dependency as others have mentioned. Backbone apps done right will run lightning fast anyways. :D

If you have any other questions let me know. I've been really enjoying backbone so I'm trying to watch the tagged posts. But seriously. Handlebars looks legit.

EDIT:

I also meant to add that the documentation for handlebars looks way more legit than underscores...

OTHER TIPS

The biggest con I can think of is that you're adding yet another library to do something your dependencies already do. If syntax is your biggest concern the following snippet will tell underscore to use a similar syntax.

_.templateSettings = {
  evaluate : /\{\[([\s\S]+?)\]\}/g,
  interpolate : /\{\{([\s\S]+?)\}\}/g
};

The question asks Rails, but isn't tagged so; so a con is conflicts with languages using mustache-like syntax such as django's templates.

If a django template parses a block first, it will attempt to fill in the {{ }} blocks before ever writing the JS.

I am using a verbatim django template tag that ignores {{}} blocks to solve the issue but I now wish I used the default <%=%> syntax so that I don't have to explicitly escape these blocks written in the django template engine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top