Sha256: 9b7df7eaaa9ebd5846df2b3c234ae0b39c0be93a59d74f76925e835c77406071

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

require 'glue/attribute'

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

4 entries across 4 versions & 1 rubygems

Version Path
nitro-0.25.0 lib/nitro/helper/buffer.rb
nitro-0.26.0 lib/nitro/helper/buffer.rb
nitro-0.27.0 lib/nitro/helper/buffer.rb
nitro-0.28.0 lib/nitro/helper/buffer.rb