Sha256: e93a0093b4ac363eb76e91aa78000e86e4de937540453c45ab435d83e31dd6fe

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 KB

Contents

# Global rendering buffer.
# Output capture functionality for ERb and Haml blocks.
#
module Aerogel::Render
module OutputBuffer

  # Returns instance of a string buffer
  #
  def self.buffer
    @buffer
  end

  # Sets instance of a string buffer
  #
  def self.buffer=( text )
    @buffer = text
  end

  # Saves current buffer in a stack and creates a new empty one.
  #
  def self.push
    @buffer_stack ||= []
    @buffer_stack.unshift @buffer
    @buffer = ''
  end

  # Returns current buffer contents as a string and restores previous
  # buffer state from a stack.
  #
  def self.pop
    result = @buffer
    @buffer = @buffer_stack.shift
    result
  end

  # Executes block and captures content rendered into buffer.
  # If no content is rendered into buffer, returns block yield result as a content.
  #
  def self.capture(*args, &block)
    self.push
    blk_result = yield *args
    buf_result = self.pop
    buf_result.nil? || buf_result.empty? ? blk_result : buf_result
  end

  # Returns true if block is defined within a ERb template.
  #
  def self.block_is_erb?( block )
    !@buffer.nil? || block && eval('defined? __in_erb_template', block.binding)
  end

  # Returns true if block is defined within a ERb template.
  #
  def self.block_is_haml?( block )
    block && eval('block_is_haml?(self)', block.binding)
  end

  # Returns true if block is defined within any template.
  #
  def self.block_is_template?( block )
    block && ( block_is_erb?(block) || block_is_haml?( block ) )
  end

end # module OutputBuffer
end # module Aerogel::Render

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
aerogel-core-1.4.12 lib/aerogel/core/render/output_buffer.rb
aerogel-core-1.4.11 lib/aerogel/core/render/output_buffer.rb
aerogel-core-1.4.10 lib/aerogel/core/render/output_buffer.rb
aerogel-core-1.3.0 lib/aerogel/core/render/output_buffer.rb
aerogel-core-1.1.1 lib/aerogel/core/render/output_buffer.rb
aerogel-core-1.1.0 lib/aerogel/core/render/output_buffer.rb