Skip to content

Instantly share code, notes, and snippets.

@thegrubbsian
Created December 14, 2012 20:13
Show Gist options
  • Save thegrubbsian/4288284 to your computer and use it in GitHub Desktop.
Save thegrubbsian/4288284 to your computer and use it in GitHub Desktop.
Simple filter for Rake Pipeline that will compile ERB/JST style templates into a templates.js file.
require "ejs"
class JstFilter < Rake::Pipeline::Filter
def initialize(output_filename=nil, &block)
block = proc { output_filename }
super(&block)
end
def generate_output(inputs, output)
template_hash = {}
inputs.each do |file|
build_templates_hash(file, template_hash)
end
output_string = build_output(template_hash)
output.write output_string
end
private
def build_templates_hash(file, template_hash)
path = file.path.gsub(/\.jst/, "")
template_hash[path] = EJS.compile(file.read)
end
def build_output(template_hash)
str = "var Templates = {};\n"
template_hash.each do |k, v|
str << "Templates[\"#{k}\"] = #{v};\n"
end
str
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment