Module: Sinatra::MarkupPlugin::OutputHelpers
- Defined in:
- lib/sinatra/markup_plugin/output_helpers.rb
Instance Method Summary collapse
-
#block_is_template?(block) ⇒ Boolean
Returns true if the block is from an ERB or HAML template; false otherwise.
-
#capture_html(*args, &block) ⇒ Object
Captures the html from a block of template code for erb or haml capture_html(&block) => “…html…”.
-
#concat_content(text = "") ⇒ Object
Outputs the given text to the templates buffer directly concat_content(“This will be output to the template buffer in erb or haml”).
-
#content_for(key, &block) ⇒ Object
Capture a block of content to be rendered at a later time.
-
#yield_content(key, *args) ⇒ Object
Render the captured content blocks for a given key.
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 block_is_template?(block)
32 33 34 |
# File 'lib/sinatra/markup_plugin/output_helpers.rb', line 32 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 capture_html(&block) => “…html…”
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/sinatra/markup_plugin/output_helpers.rb', line 6 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 concat_content(“This will be output to the template buffer in erb or haml”)
19 20 21 22 23 24 25 26 27 |
# File 'lib/sinatra/markup_plugin/output_helpers.rb', line 19 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, &block) ⇒ Object
Capture a block of content to be rendered at a later time. Your blocks can also receive values, which are passed to them by yield_content
content_for(:name) { …content… } content_for(:name) { |name| …content… }
40 41 42 |
# File 'lib/sinatra/markup_plugin/output_helpers.rb', line 40 def content_for(key, &block) content_blocks[key.to_sym] << block 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. yield_content :include yield_content :head, “param1”, “param2”
49 50 51 52 53 |
# File 'lib/sinatra/markup_plugin/output_helpers.rb', line 49 def yield_content(key, *args) content_blocks[key.to_sym].map { |content| capture_html(*args, &content) }.join end |