Skip to content

Instantly share code, notes, and snippets.

@tarun-pacifica
Last active June 9, 2016 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarun-pacifica/a7c5c2993d40df4f39691b3cd35be367 to your computer and use it in GitHub Desktop.
Save tarun-pacifica/a7c5c2993d40df4f39691b3cd35be367 to your computer and use it in GitHub Desktop.
var savefy = function() {
//grab all the script files in your browser with type "text/template", stored as an array
var templates = document.querySelectorAll('script[type="text/template"]');
//iterate through each template
_.each(templates, function(template) {
if (template.id) {
//grab the id of the template and store as a handlebars filename
var filename = (template.id + '.hbs').toString();
//store the contents of the template....
var contents = $(template).html();
//...and create a new file
var newfile = new Blob([contents]);
//save to disk
saveAs(newfile, filename);
}
});
};
@tarun-pacifica
Copy link
Author

tarun-pacifica commented Jun 9, 2016

I wrote this function as part of a Backbone.js perfomance optimisation project to implement precompilation of templates.It saved a tonne of time with a Backbone app which contained over 40 templates.

Use savefy.js to automate the conversion process of templates from existing scripts. Run this in your browser console. Dependencies are standard Backbone.js (Underscore.js, jQuery) plus the FileSaver.js library. (https://github.com/eligrey/FileSaver.js).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment