Module: Sinatra::OutputBuffer::Helpers

Defined in:
lib/sinatra/outputbuffer.rb

Instance Method Summary collapse

Instance Method Details

#block_is_template?(block) ⇒ Boolean

Returns true if the block is from an ERB or HAML template; false otherwise. Used to determine if html should be returned or concatted to view

Examples

block_is_template?(block)

Returns:

  • (Boolean)


118
119
120
# File 'lib/sinatra/outputbuffer.rb', line 118

def block_is_template?(block) 
   block && (block_is_erb?(block) || (self.respond_to?(:block_is_haml?) && block_is_haml?(block)))
end

#capture_html(*args, &block) ⇒ Object

Captures the html from a block of template code for erb or haml

Examples

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


80
81
82
83
84
85
86
87
88
89
# File 'lib/sinatra/outputbuffer.rb', line 80

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

#concat_content(text = "") ⇒ Object

Outputs the given text to the templates buffer directly

Examples

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


99
100
101
102
103
104
105
106
107
# File 'lib/sinatra/outputbuffer.rb', line 99

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

#content_for(key, content = nil, &block) ⇒ Object

Capture a block or text of content to be rendered at a later time. Your blocks can also receive values, which are passed to them by yield_content

Examples

content_for(:name) { ...content... }
content_for(:name) { |name| ...content... }
content_for(:name, "I'm Jeff")


133
134
135
# File 'lib/sinatra/outputbuffer.rb', line 133

def content_for(key, content = nil, &block) 
  content_blocks[key.to_sym] << (block_given? ? block : Proc.new { content })
end

#yield_content(key, *args) ⇒ Object

Render the captured content blocks for a given key. You can also pass values to the content blocks by passing them as arguments after the key.

Examples

yield_content :include
yield_content :head, "param1", "param2"
yield_content(:title) || "My page title"


149
150
151
152
153
154
155
# File 'lib/sinatra/outputbuffer.rb', line 149

def yield_content(key, *args) 
  blocks = content_blocks[key.to_sym]
  return nil if blocks.empty?
  blocks.map { |content|
    capture_html(*args, &content)
  }.join
end