Skip to content

Commit

Permalink
Haml generates template_source and can be used with compiled templates
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed Mar 4, 2010
1 parent 3a6e53f commit 5e0c907
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
26 changes: 24 additions & 2 deletions lib/tilt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,33 @@ def prepare
@engine = ::Haml::Engine.new(data, haml_options)
end

def evaluate(scope, locals, &block)
@engine.render(scope, locals, &block)
# 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 template_source
@engine.instance_eval do
<<-RUBY
_haml_locals = locals
begin
extend Haml::Helpers
_hamlout = @haml_buffer = Haml::Buffer.new(@haml_buffer, #{options_for_buffer.inspect})
_erbout = _hamlout.buffer
__in_erb_template = true
#{precompiled}
#{precompiled_method_return_value}
ensure
@haml_buffer = @haml_buffer.upper
end
RUBY
end
end

private
def local_assignment_code(locals)
source, offset = super
[source, offset + 6]
end

def haml_options
options.merge(:filename => eval_file, :line => line)
end
Expand Down
53 changes: 53 additions & 0 deletions test/tilt_hamltemplate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,59 @@ class HamlTemplateTest < Test::Unit::TestCase
end
end

class CompiledHamlTemplateTest < Test::Unit::TestCase
class Scope
include Tilt::CompileSite
end

test "passing locals" do
template = Tilt::HamlTemplate.new { "%p= 'Hey ' + name + '!'" }
assert_equal "<p>Hey Joe!</p>\n", template.render(Scope.new, :name => 'Joe')
end

test "evaluating in an object scope" do
template = Tilt::HamlTemplate.new { "%p= 'Hey ' + @name + '!'" }
scope = Scope.new
scope.instance_variable_set :@name, 'Joe'
assert_equal "<p>Hey Joe!</p>\n", template.render(scope)
end

test "passing a block for yield" do
template = Tilt::HamlTemplate.new { "%p= 'Hey ' + yield + '!'" }
assert_equal "<p>Hey Joe!</p>\n", template.render(Scope.new) { 'Joe' }
end

test "backtrace file and line reporting without locals" do
data = File.read(__FILE__).split("\n__END__\n").last
fail unless data[0] == ?%
template = Tilt::HamlTemplate.new('test.haml', 10) { data }
begin
template.render(Scope.new)
fail 'should have raised an exception'
rescue => boom
assert_kind_of NameError, boom
line = boom.backtrace.first
file, line, meth = line.split(":")
assert_equal 'test.haml', file
assert_equal '12', line
end
end

test "backtrace file and line reporting with locals" do
data = File.read(__FILE__).split("\n__END__\n").last
fail unless data[0] == ?%
template = Tilt::HamlTemplate.new('test.haml') { data }
begin
res = template.render(Scope.new, :name => 'Joe', :foo => 'bar')
rescue => boom
assert_kind_of MockError, boom
line = boom.backtrace.first
file, line, meth = line.split(":")
assert_equal 'test.haml', file
assert_equal '5', line
end
end
end
rescue LoadError => boom
warn "Tilt::HamlTemplate (disabled)\n"
end
Expand Down

0 comments on commit 5e0c907

Please sign in to comment.