Skip to content

Instantly share code, notes, and snippets.

@ahmetkizilay
Created May 19, 2015 17:03
Show Gist options
  • Save ahmetkizilay/d0564351f91a0af08ff6 to your computer and use it in GitHub Desktop.
Save ahmetkizilay/d0564351f91a0af08ff6 to your computer and use it in GitHub Desktop.
compiling handlebars with rake
task :handlebars => :environment do
puts "Compiling Handlebars templates"
files = Rake::FileList["app/assets/javascripts/templates/**/*.hbs"]
compiled_js = ""
files.each do |file|
openFile = File.open(file)
pathName = openFile.to_path.sub("app/assets/javascripts/templates/", "")[0...-4]
content = openFile.read
compiled = HandlebarsAssets::Handlebars.precompile(content, HandlebarsAssets::Config.options)
compiled_js += "(function() {\nthis.HandlebarsTemplates || (this.HandlebarsTemplates = {});\nthis.HandlebarsTemplates[\"" + pathName + "\"] = Handlebars.template(" + compiled + ");\n}).call(this);\n\n\n"
end
output = File.new("app/assets/javascripts/templates/compiled.js", "w")
output.puts(compiled_js)
output.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment