Created
December 14, 2012 20:13
-
-
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.
This file contains hidden or 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
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