Skip to content

Commit

Permalink
Merge branch 'exposebuf'
Browse files Browse the repository at this point in the history
  • Loading branch information
rtomayko committed May 28, 2010
2 parents 8005323 + ca0c47a commit d2274c1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/tilt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,18 +350,29 @@ def precompiled_template(locals)
# ERB template implementation. See:
# http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html
class ERBTemplate < Template
@@default_output_variable = '_erbout'

def self.default_output_variable
@@default_output_variable
end

def self.default_output_variable=(name)
@@default_output_variable = name
end

def initialize_engine
return if defined? ::ERB
require_template_library 'erb'
end

def prepare
@outvar = (options[:outvar] || '_erbout').to_s
@outvar = options[:outvar] || self.class.default_output_variable
@engine = ::ERB.new(data, options[:safe], options[:trim], @outvar)
end

def precompiled_template(locals)
@engine.src
source = @engine.src
source
end

def precompiled_preamble(locals)
Expand Down Expand Up @@ -414,7 +425,7 @@ def initialize_engine

def prepare
@options.merge!(:preamble => false, :postamble => false)
@outvar = (options.delete(:outvar) || '_erbout').to_s
@outvar = options.delete(:outvar) || self.class.default_output_variable
engine_class = options.delete(:engine_class)
engine_class = ::Erubis::EscapedEruby if options.delete(:escape_html)
@engine = (engine_class || ::Erubis::Eruby).new(data, options)
Expand Down
17 changes: 17 additions & 0 deletions test/tilt_erbtemplate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ class ERBTemplateTest < Test::Unit::TestCase
assert_equal "Hey Joe!", template.render(scope)
end

class MockOutputVariableScope
attr_accessor :exposed_buffer
end

test "exposing the buffer to the template by default" do
begin
Tilt::ERBTemplate.default_output_variable = '@_out_buf'
template = Tilt::ERBTemplate.new { '<% self.exposed_buffer = @_out_buf %>hey' }
scope = MockOutputVariableScope.new
template.render(scope)
assert_not_nil scope.exposed_buffer
assert_equal scope.exposed_buffer, 'hey'
ensure
Tilt::ERBTemplate.default_output_variable = '_erbout'
end
end

test "passing a block for yield" do
template = Tilt::ERBTemplate.new { 'Hey <%= yield %>!' }
assert_equal "Hey Joe!", template.render { 'Joe' }
Expand Down
17 changes: 17 additions & 0 deletions test/tilt_erubistemplate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ class ErubisTemplateTest < Test::Unit::TestCase
assert_equal "Hey Joe!", template.render(scope)
end

class MockOutputVariableScope
attr_accessor :exposed_buffer
end

test "exposing the buffer to the template by default" do
begin
Tilt::ErubisTemplate.default_output_variable = '@_out_buf'
template = Tilt::ErubisTemplate.new { '<% self.exposed_buffer = @_out_buf %>hey' }
scope = MockOutputVariableScope.new
template.render(scope)
assert_not_nil scope.exposed_buffer
assert_equal scope.exposed_buffer, 'hey'
ensure
Tilt::ErubisTemplate.default_output_variable = '_erbout'
end
end

test "passing a block for yield" do
template = Tilt::ErubisTemplate.new { 'Hey <%= yield %>!' }
assert_equal "Hey Joe!", template.render { 'Joe' }
Expand Down

0 comments on commit d2274c1

Please sign in to comment.