Re-Learning Backbone.js – Templates (Underscore)

Even though templates are not part of Backbone.js, I believe it’s critical to have a basic understanding of templates. Templates are a core component that will be used with Backbone.js Views.

Templates will help us separate code from the HTML markup. When MVC is discussed in regards to server side (Ruby on Rails, ASP.NET MVC) the “V” is for view. Views are implemented different in Backbone than in most server side technologies. On the server side, views should be extremely dumb and very little logic if any should be include.

Backbone.js has a key object called View. The Backbone.js View does not resemble server side views. Backbone.js Views are not dumb and they do have logic. Some people have described Backbone.js Views as being more similar to Controllers on server side. I believe that templates on the client (JavaScript) side work more like the views on the server side. It’s definitely a mind shift when moving from a server MVC mindset to a client side MVC mindset. Hopefully this will make the transition easier.

I want to keep this as simple as possible. We will take very small steps to help you learn about templates. Only 2 JavaScript libraries will be needed, jQuery and Underscore. We will not be using the Backbone.js library. There are many options for templating, but we will use the built in option that Underscore provides. In case you didn’t know, Backbone.js has a dependency on Underscore. This is why we are using Underscore’s template engine, but other template engines can be substituted for the Underscore template engine.

Very, Very Simple Template

Continue reading “Re-Learning Backbone.js – Templates (Underscore)”