1

I would like to have all Backbone templates in a one single JS file so that I can easily distribute it through CDN. Ideally it should also work with Rails asset compile workflow.

Possible solution #1:

A single JS file with all the templates declared as such:

app.templates.view = " \
<h3>something code</h3> \
";

Too messy. Lack of highlighting.

Possible solution #2:

A single html file with all the templates inline. Include the partial in my master document.

<script type="text/template" id="list-item">
    <li><%= item %></li>
</script>

I cannot use the benefit of minified JS file that is distributed through my CDN. The templates will be inline with my HTML file.

Possible solution #3:

Use Require.js but it does not seem to solve the need of including all templates in a single JS file.

Min Ming Lo
  • 2,398
  • 2
  • 18
  • 25
  • what about something like this grunt-task? https://github.com/yeoman/grunt-usemin but that wouldnt work with the rails asset pipeline... – hereandnow78 Feb 27 '13 at 01:06
  • 1
    ["The **source** property is available on the compiled template function for easy precompilation."](http://underscorejs.org/#template) so you could easily precompile your Underscore templates into a single JavaScript file. – mu is too short Feb 27 '13 at 02:25
  • you can use grunt to generate module from html template,concat and compress the template js files. you can look this [answer](http://stackoverflow.com/questions/14923592/backbone-requirejs-html-files-loaded-with-requirejs-are-interpreted-as-js-fil/14971014#14971014) – anhulife Feb 27 '13 at 02:29

2 Answers2

0

Take a look at Lineman. It gives you a really nice workflow for developing JavaScript clients. It is really just a collection of Grunt tasks aggregated into a super-simple workflow. One of the things it does is compile all of your Underscore (or Handlebars) templates into JSTs (like Rails). It uses "grunt-contrib-jst" to do this and outputs the templates to "generated/template/underscore.js". It then compiles all of your JS into a single file, though you could easily bring in the .js files separately.

It might point you in the right direction.

Brian Genisio
  • 47,787
  • 16
  • 124
  • 167
0

Thanks for the responses. I ended with with Possible solution #2 where I have a bunch of templates and included in a master file using Rails partial. For now there are no much benefit in distributing the templates through CDN.

*I did not choose to use Grunt because I think it adds additional complexity in the workflow, considering that all devs in the team have to adopt the new workflow.

Min Ming Lo
  • 2,398
  • 2
  • 18
  • 25