module Padrino::Helpers::OutputHelpers

def block_is_erb?(block)

and implicitly concat.
can't take an <% end %> later on, so we have to use <% ... %>
We'd return a string in any other case, but erb <%= ... %>
Check whether we're called from an erb template.
def block_is_erb?(block)
  has_erb_buffer? || block && eval('defined? __in_erb_template', block)
end

def block_is_erb?(block)

def block_is_erb?(block)
  has_erb_buffer? || block && eval('defined? __in_erb_template', block.binding)
end

def block_is_template?(block)


block_is_template?(block)

==== Examples

Used to determine if html should be returned or concatted to view
Returns true if the block is from an ERB or HAML template; false otherwise.
#
def block_is_template?(block)
  block && (block_is_erb?(block) || (self.respond_to?(:block_is_haml?) && block_is_haml?(block)))
end

def capture_erb(*args, &block)


capture_erb(&block) => '...html...'

==== Examples

Used to capture the html from a block of erb code
#
def capture_erb(*args, &block)
  erb_with_output_buffer { block_given? && block.call(*args) }
end

def capture_html(*args, &block)


capture_html(&block) => "...html..."

==== Examples

Captures the html from a block of template code for erb or haml
#
def capture_html(*args, &block)
  if self.respond_to?(:is_haml?) && is_haml?
    block_is_haml?(block) ? capture_haml(*args, &block) : block.call
  elsif has_erb_buffer?
    result_text = capture_erb(*args, &block)
    result_text.present? ? result_text : (block_given? && block.call(*args))
  else # theres no template to capture, invoke the block directly
    block.call(*args)
  end
end

def concat_content(text="")


concat_content("This will be output to the template buffer in erb or haml")

==== Examples

Outputs the given text to the templates buffer directly
#
def concat_content(text="")
  if self.respond_to?(:is_haml?) && is_haml?
    haml_concat(text)
  elsif has_erb_buffer?
    erb_concat(text)
  else # theres no template to concat, return the text directly
    text
  end
end

def content_blocks


content_blocks[:name] => ['...', '...']

==== Examples

Retrieves content_blocks stored by content_for or within yield_content
#
def content_blocks
  @content_blocks ||= Hash.new {|h,k| h[k] = [] }
end

def content_for(key, &block)


content_for(:name) { |name| ...content... }
content_for(:name) { ...content... }

==== Examples

Your blocks can also receive values, which are passed to them by yield_content
Capture a block of content to be rendered at a later time.
#
def content_for(key, &block)
  content_blocks[key.to_sym] << block
end

def erb_concat(text)


erb_concat("Direct to buffer")

==== Examples

Concats directly to an erb template
#
def erb_concat(text)
  @_out_buf << text if has_erb_buffer?
end

def erb_with_output_buffer(buf = '')


Used to direct the buffer for the erb capture
#
def erb_with_output_buffer(buf = '')
  @_out_buf, old_buffer = buf, @_out_buf
  yield
  @_out_buf
ensure
  @_out_buf = old_buffer
end

def has_erb_buffer?


has_erb_buffer? => true

==== Examples

Returns true if an erb buffer is detected
#
def has_erb_buffer?
  !@_out_buf.nil?
end

def yield_content(key, *args)


yield_content :head, "param1", "param2"
yield_content :include

==== Examples

as arguments after the key.
You can also pass values to the content blocks by passing them
Render the captured content blocks for a given key.
#
def yield_content(key, *args)
  content_blocks[key.to_sym].map { |content|
    capture_html(*args, &content)
  }.join
end