Building Applications with Backbone and Laravel: JST

Posted 13th April 2014

You might well be used to devel­op­ing Back­bone views like this:

MyApp.Views.SomeView = Backbone.View.extend({    
  template: _.template('<ul><% _.each(items, function(item){ %><li><%= item %></li><% }); %></ul>'),    
  el: '#some-element',

This is all fine and well for sim­ple struc­tures, but can quickly get unwieldly if you’re build­ing markup of any sig­nif­i­cant size or com­plex­ity. It can also lead to all sorts of issues around readability.

One alter­na­tive is to put more com­plex tem­plates into sep­a­rate HTML files:

// file: folder/template.html

<ul>
 <% _.each(items, function(item){ %>
 <li> <%= item %> </li>
 <% }); %>
</ul>