Created
May 2, 2011 21:31
-
-
Save aiwilliams/952416 to your computer and use it in GitHub Desktop.
Sprockets producing Handlebars templates from Haml
This file contains 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
http_path = "/" | |
css_dir = "public/stylesheets" | |
sass_dir = "sass" | |
images_dir = "public/images" | |
http_images_dir = "images" | |
javascripts_dir = "public/javascripts" | |
fonts_dir = "public/fonts" | |
http_fonts_dir = "fonts" | |
line_comments = false |
This file contains 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
$LOAD_PATH << File.expand_path('.') | |
require 'rubygems' | |
require 'bundler/setup' | |
require 'haml' | |
require 'sass' | |
require 'sass/plugin/rack' | |
require 'compass' | |
require 'tilt' | |
require 'sprockets' | |
require 'hamlbars' | |
Compass.add_configuration(File.expand_path('config.rb')) | |
Compass.configure_sass_plugin! | |
use Sass::Plugin::Rack | |
map '/javascripts' do | |
sprockets = Sprockets::Environment.new | |
sprockets.engines.register 'hamlbars', Tilt::HamlbarsTemplate | |
sprockets.paths << 'apps' | |
sprockets.paths << 'vendor/javascripts' | |
sprockets.static_root = 'public/javascripts' | |
run sprockets | |
end | |
map '/' do | |
run Rack::Directory.new('public') | |
end |
This file contains 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 'tilt/template' | |
module Tilt | |
class HamlbarsTemplate < Template | |
JS_ESCAPE_MAP = { | |
"\r\n" => '\n', | |
"\n" => '\n', | |
"\r" => '\n', | |
'"' => '\\"', | |
"'" => "\\'" } | |
self.default_mime_type = 'application/javascript' | |
def self.engine_initialized? | |
defined? ::Haml::Engine | |
end | |
def initialize_engine | |
require_template_library 'haml' | |
end | |
def prepare | |
options = @options.merge(:filename => eval_file, :line => line) | |
@engine = ::Haml::Engine.new(data, options) | |
end | |
def evaluate(scope, locals, &block) | |
template = if @engine.respond_to?(:precompiled_method_return_value, true) | |
super | |
else | |
@engine.render(scope, locals, &block) | |
end | |
if basename =~ /^_/ | |
"Handlebars.registerPartial('#{name}', '#{template.strip.gsub(/(\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }}');\n" | |
else | |
"App.registerTemplate('#{name}', '#{template.strip.gsub(/(\r\n|[\n\r"'])/) { JS_ESCAPE_MAP[$1] }}');\n" | |
end | |
end | |
# Precompiled Haml source. Taken from the precompiled_with_ambles | |
# method in Haml::Precompiler: | |
# http://github.com/nex3/haml/blob/master/lib/haml/precompiler.rb#L111-126 | |
def precompiled_template(locals) | |
@engine.precompiled | |
end | |
def precompiled_preamble(locals) | |
local_assigns = super | |
@engine.instance_eval do | |
<<-RUBY | |
begin | |
extend Haml::Helpers | |
_hamlout = @haml_buffer = Haml::Buffer.new(@haml_buffer, #{options_for_buffer.inspect}) | |
_erbout = _hamlout.buffer | |
__in_erb_template = true | |
_haml_locals = locals | |
#{local_assigns} | |
RUBY | |
end | |
end | |
def precompiled_postamble(locals) | |
@engine.instance_eval do | |
<<-RUBY | |
#{precompiled_method_return_value} | |
ensure | |
@haml_buffer = @haml_buffer.upper | |
end | |
RUBY | |
end | |
end | |
end | |
end | |
module Haml | |
module Helpers | |
module HamlbarsExtensions | |
def iterate(name, &block) | |
content = capture_haml(&block) | |
"{{##{name}}}#{content.strip}{{/#{name}}}" | |
end | |
end | |
include HamlbarsExtensions | |
end | |
end | |
That's great news. Thanks for the heads up and all your excellent work.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I just started working on making sprockets support sass better. It should land in the next few days: sstephenson/sprockets#23