Module: Padrino::Helpers::OutputHelpers

Included in:
Middleman::MetaPages::ConfigSetting, Middleman::MetaPages::SitemapResource, MiniTest::Spec
Defined in:
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers/erb_handler.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers/haml_handler.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers/slim_handler.rb,
lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers/abstract_handler.rb

Overview

Helpers related to buffer output for various template engines.

Defined Under Namespace

Modules: SinatraCurrentEngine Classes: AbstractHandler, ErbHandler, HamlHandler, SlimHandler

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.handlersObject

Returns the list of all available template handlers

Examples:

OutputHelpers.handlers => [<OutputHelpers::HamlHandler>, <OutputHelpers::ErbHandler>]


11
12
13
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers/abstract_handler.rb', line 11

def self.handlers
  @_template_handlers ||= []
end

.included(base) ⇒ Object



8
9
10
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers.rb', line 8

def self.included(base) # @private
  base.send(:include, SinatraCurrentEngine) unless base.method_defined?(:current_engine)
end

.register(handler) ⇒ Object

Registers a new handler as available to the output helpers

Examples:

OutputHelpers.register(OutputHelpers::HamlHandler)


22
23
24
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers/abstract_handler.rb', line 22

def self.register(handler)
  handlers << handler
end

Instance Method Details

#block_is_template?(block) ⇒ Boolean

Returns true if the block is from a supported template type; false otherwise. Used to determine if html should be returned or concatenated to the view.

Examples:

block_is_template?(block) => true

Parameters:

  • block (Block)

    Determine if this block is a view template.

Returns:

  • (Boolean)

    True if the block is a template; false otherwise.



111
112
113
114
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers.rb', line 111

def block_is_template?(block)
  handler = find_proper_handler
  block && handler && handler.block_is_type?(block)
end

#capture_html(*args, &block) ⇒ String Also known as: capture

Captures the html from a block of template code for any available handler.

Be aware that trusting the html is up to the caller.

Examples:

capture_html(&block) => "...html..."
capture_html(object_for_block, &block) => "...html..."
ActiveSupport::SafeBuffer.new + capture_html { "<foo>" }
# => "&lt;foo&gt;"
ActiveSupport::SafeBuffer.safe_concat + capture_html { "<foo>" }
# => "<foo>"

Parameters:

  • *args (Object)

    Objects yield to the captured block

  • &block (Proc)

    Template code to capture as html

Returns:

  • (String)

    Captured html resulting from the block



49
50
51
52
53
54
55
56
57
58
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers.rb', line 49

def capture_html(*args, &block)
  handler = find_proper_handler
  captured_block, captured_html = nil, ""
  if handler && handler.is_type? && handler.block_is_type?(block)
    captured_html, captured_block = handler.capture_from_template(*args, &block)
  end
  # invoking the block directly if there was no template
  captured_html = block_given? && ( captured_block || block.call(*args) )  if captured_html.blank?
  captured_html
end

#concat_content(text = "") ⇒ Object Also known as: concat

Outputs the given text to the templates buffer directly.

The output might be subject to escaping, if it is not marked as safe.

Examples:

concat_content("This will be output to the template buffer")

Parameters:

  • text (String, SafeBuffer) (defaults to: "")

    Text to concatenate to the buffer.



73
74
75
76
77
78
79
80
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers.rb', line 73

def concat_content(text="")
  handler = find_proper_handler
  if handler && handler.is_type?
    handler.concat_to_template(text)
  else # theres no template to concat, return the text directly
    text
  end
end

#concat_safe_content(text = "") ⇒ Object

Outputs the given text to the templates buffer directly, assuming that it is safe.

Examples:

concat_safe_content("This will be output to the template buffer")

Parameters:

  • text (String) (defaults to: "")

    Text to concatenate to the buffer.



94
95
96
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers.rb', line 94

def concat_safe_content(text="")
  concat_content text.html_safe
end

#content_for(key, content) ⇒ Object #content_for(key, &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")

Overloads:

  • #content_for(key, content) ⇒ Object

    Parameters:

    • key (Symbol)

      Name of your key for the content yield.

    • content (String)

      Text to be stored for this key.

  • #content_for(key, &block) ⇒ Object

    Parameters:

    • key (Symbol)

      Name of your key for the content yield.

    • block (Proc)

      Block to be stored as content for this key.



133
134
135
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers.rb', line 133

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

#content_for?(key) ⇒ TrueClass, FalseClass

Is there a content block for a given key?

Examples:

content_for? :header => true

Parameters:

  • key (Symbol)

    Name of content to yield

Returns:

  • (TrueClass, FalseClass)

    Result html for the given key



149
150
151
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers.rb', line 149

def content_for?(key)
  content_blocks[key.to_sym].present?
end

#yield_content(key, *args) ⇒ String

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"

Parameters:

  • key (Symbol)

    Name of content to yield

  • *args

    Values to pass to the content block

Returns:

  • (String)

    Result html for the given key



171
172
173
174
175
# File 'lib/vendored-middleman-deps/padrino-helpers-0.11.2/lib/padrino-helpers/output_helpers.rb', line 171

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