Skip to content

Instantly share code, notes, and snippets.

@nzifnab
Created May 7, 2011 08:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nzifnab/960329 to your computer and use it in GitHub Desktop.
Save nzifnab/960329 to your computer and use it in GitHub Desktop.
Monkeypatch of Jammit to pre-compile using Haml
module Jammit
class Compressor
# Compiles a single JST file by writing out a javascript that adds
# template properties to a top-level template namespace object. Adds a
# JST-compilation function to the top of the package, unless you've
# specified your own preferred function, or turned it off.
# JST templates are named with the basename of their file.
# We are monkey-patching it so that we get Haml compilation *before* it hits the client.
def compile_jst(paths)
namespace = Jammit.template_namespace
paths = paths.grep(Jammit.template_extension_matcher).sort
base_path = find_base_path(paths)
compiled = paths.map do |path|
contents = read_binary_file(path)
#contents = contents.gsub(/\r?\n/, "\\n").gsub("'", '\\\\\'')
contents = Haml::Engine.new(contents).render.gsub(/\r?\n/, "\\n").gsub("'", '\\\\\'')
name = template_name(path, base_path)
"#{namespace}['#{name}'] = #{Jammit.template_function}('#{contents}');"
end
compiler = Jammit.include_jst_script ? read_binary_file(DEFAULT_JST_SCRIPT) : '';
setup_namespace = "#{namespace} = #{namespace} || {};"
[JST_START, setup_namespace, compiler, compiled, JST_END].flatten.join("\n")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment