1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
module CaptureHelper

  def capture_erb(*args, &block)
    buffer = eval('_erbout', block.binding)
    capture_erb_with_buffer(buffer, *args, &block)
  end
  alias_method :capture, :capture_erb
  alias_method :capture_block, :capture_erb

  def capture_erb_with_buffer(buffer, *args, &block)
    pos = buffer.length
    block.call(*args)
    # extract the block 
    data = buffer[pos..-1]
    # replace it in the original with empty string
    buffer[pos..-1] = ''
    data
  end  
end  # module CaptureHelper

Webby::Helpers.register(CaptureHelper)