Skip to content

Instantly share code, notes, and snippets.

@pkieltyka
Created August 13, 2012 21:11
Show Gist options
  • Save pkieltyka/921174ac19790da21b42 to your computer and use it in GitHub Desktop.
Save pkieltyka/921174ac19790da21b42 to your computer and use it in GitHub Desktop.
def compile_templates
unless @context
if RUBY_ENGINE == 'jruby'
@context = Rhino::Context.new
else
@context = V8::Context.new
end
handlebars_js = AppApi.root.join("public/javascripts/handlebars.js").to_s
@context.load(handlebars_js)
end
js = "var templates = {};\n"
@template_files.each do |template_path|
name = File.basename(template_path).split('.')[0]
template = IO.read(template_path).force_encoding("UTF-8")
js << "templates['#{name}'] = Handlebars.precompile('#{escape_javascript(template)}');\n"
end
js << "templates"
# TODO: .. 64K script limit...? .. this kinda screws us... because it makes our scripts pretty big..
# TODO: Java out of memory
data = @context.eval(js)
response = "var App = App || {};\App.TEMPLATES = {};\n"
data.each do |k,v|
response << "App.TEMPLATES['#{k}'] = #{v.to_s};\n"
end
response
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment