Sha256: 8c740890367a439b5e8a7b79fa0a72d9f7af672edcf3e5d3f5e9127e3ef694e6

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module Nitro

# The output buffering mixin. Provides php-style output
# buffering functionality.
#
# === Examples
#
# <?r buf = capture do ?>
# ...
# <?r end ?>
#--
# TODO: use better names but keep the ob_xxx php style methods
# as aliases.
#++

module BufferHelper

private

  # Output buffers stack, used for php-style nested output 
  # buffering.
  
  def out_buffers; @out_buffers; end
  
  # Start (push) a new output buffer.

  def open_buffer
    @out_buffers ||= []
    @out_buffers.push(@out)
    @out = ''
  end
  alias_method :ob_start, :open_buffer
  
  # End (pop) the current output buffer.
  
  def close_buffer
    buf = @out
    @out = @out_buffers.pop
    return buf
  end
  alias_method :ob_end, :close_buffer
  
  # End (pop) the current output buffer and write to the parent.
  
  def close_and_write_buffer
    nested_buffer = @out
    @out = @out_buffers.pop
    @out << nested_buffer
  end
  alias_method :ob_write_end, :close_and_write_buffer
  
  def capture
    open_buffer
    yield
    return close_buffer
  end
  
end

end

# * George Moschovitis <gm@navel.gr>

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
nitro-0.29.0 lib/nitro/helper/buffer.rb
nitro-0.30.0 lib/nitro/helper/buffer.rb
nitro-0.31.0 lib/nitro/helper/buffer.rb