Skip to content

Instantly share code, notes, and snippets.

@willbailey
Created July 24, 2009 13:50
Show Gist options
  • Save willbailey/154229 to your computer and use it in GitHub Desktop.
Save willbailey/154229 to your computer and use it in GitHub Desktop.
# Renders all the JST and returns the Javascript TemplateCache
class TemplateCache
def generate(opts={})
defaults = {:noisy => false}
options = defaults.merge(opts)
jst_files, templates, errors = [],[],[]
view_path = Rails::Configuration.new.view_path
view = ActionView::Base.new(view_path)
view.extend(ApplicationHelper)
# find jst files
jst_files = Dir.glob(RAILS_ROOT + "/app/views/jst/**/*.jst")
# read templates
jst_files.each do |file|
open file do |f|
template = view.render(:file => file.gsub!(/^.*\/views\//,''))
template_sections = template.split("__END__")
template_sections.each do |t|
if t.match(/__TEMPLATE_(.*)__/)
template_name = t.match(/__TEMPLATE_(.*)__/)[1]
puts template_name if options[:noisy]
template = t.gsub(/\s*__TEMPLATE_(.*)__|__END__|\n/,'')
# Replace whitespace between tags
template.gsub!(/>\s+</, '><')
template = template_name + " : '" + template.gsub(/[']/, '\\\\\'') + "',"
templates << template
end
end
end
end
cache = '{' << templates.join("\n ").chomp(',') << "\n" << '}'
return cache
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment