Skip to content

Instantly share code, notes, and snippets.

@colbyr
Created January 8, 2012 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save colbyr/1579888 to your computer and use it in GitHub Desktop.
Save colbyr/1579888 to your computer and use it in GitHub Desktop.
Backbone-Handlebars View Template
/* base view template */
var ViewTemplate = Backbone.View.extend({
// containing element
el: $('body'),
// events
events: {},
// initialize
initialize: function(){
_.bindAll(this, 'render', 'template', 'html');
throw "Error: trying to instantiate ViewTemplate - templates must be overridden sir"
},
// this overwrites all content in the element - override with append() if thats not what you're going for
render: function(){
$(this.el).html(this.html());
},
// id of the handlebars template element
template_id: '#template',
// template rendering function
template: function(id, context){
var src = $( id || this.template_id ).html();
var templ = Handlebars.compile(src);
return templ(context || this.context());
},
// contest function
context: function(){
var that = this;
// returns a handlebars context object
return {
title: 'Template View'
}
},
// returns the html for the view
html: function(){
return this.template();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment