Backbone Template
Standard fiddle
HTML
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.1/underscore-min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script>
<div id="search_container">a</div>
<script type="text/template" id="search_template">
<h1>Sample title</h1>
<h2>Sample subtitle</h2>
<label>Search</label>
<input type="text" id="search_input" />
<input type="button" id="search_button" value="Search" />
<h2>Another sample subtitle</h2>
<p>Sample paragraph</p>
<table>
<tbody>
<tr>
<th>Name</th>
<td>Tesla</td>
</tr>
</tbody>
</table>
<p>Another sample paragraph</p>
</script>
JavaScript
SearchView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
// Compile the template using underscore
var template = _.template( $("#search_template").html(), {} );
// Load the compiled HTML into the Backbone "el"
this.$el.html( template );
},
events: {
"click input[type=button]": "doSearch"
},
doSearch: function( event ){
// Button clicked, you can access the element that was clicked with event.currentTarget
alert( "Search for " + $("#search_input").val() );
}
});
var search_view = new SearchView({ el: $("#search_container") });