Last active
June 9, 2016 10:29
-
-
Save tarun-pacifica/a7c5c2993d40df4f39691b3cd35be367 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).