module Padrino::Helpers::OutputHelpers
def self.handlers
Returns the list of all available template handlers.
#
def self.handlers @_template_handlers ||= {} end
def self.included(base)
def self.included(base) base.send(:include, SinatraCurrentEngine) unless base.method_defined?(:current_engine) end
def self.register(engine, handler)
Registers a new handler as available to the output helpers.
#
def self.register(engine, handler) handlers[engine] = handler end
def block_is_template?(block)
-
(Boolean)
- True if the block is a template; false otherwise.
Parameters:
-
block
(Block
) --
def block_is_template?(block) handler = find_proper_handler block && handler && handler.engine_matches?(block) end
def capture_html(*args, &block)
-
(String)
- Captured HTML resulting from the block.
Parameters:
-
&block
(Proc
) -- -
*args
(Object
) --
def capture_html(*args, &block) if handler = find_proper_handler handler.capture_from_template(*args, &block) else yield(*args) end end
def concat_content(text="")
-
text
(String, SafeBuffer
) --
def concat_content(text="") if handler = find_proper_handler handler.concat_to_template(text, binding) else text end end
def concat_safe_content(text="")
-
text
(String
) --
def concat_safe_content(text="") concat_content text.html_safe end
def content_blocks
content_blocks[:name] => ['...', '...']
@example
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, content = nil, options = {}, &block)
(**options)
-
:flush
(Boolean
) --
Parameters:
-
options
(Hash
) -- Options associated with this method. -
block
(Proc
) -- Block to be stored as content for this key. -
key
(Symbol
) -- Name of your key for the content yield. -
options
(Hash
) -- Options associated with this method. -
content
(String
) -- Text to be stored for this key. -
key
(Symbol
) -- Name of your key for the content yield.
Overloads:
-
content_for(key, &block)
-
content_for(key, content)
def content_for(key, content = nil, options = {}, &block) options = content if content.is_a?(Hash) content_blocks[key.to_sym].clear if options[:flush] content_blocks[key.to_sym] << (block_given? ? block : Proc.new { content }) end
def content_for?(key)
-
(TrueClass, FalseClass)
- Result html for the given +key+
Parameters:
-
key
(Symbol
) --
def content_for?(key) !content_blocks[key.to_sym].empty? end
def find_proper_handler
find_proper_handler =>
@example
Can handle any output related to capturing or concatenating in a given template.
Retrieves the template handler for the given output context.
#
def find_proper_handler handler_class = OutputHelpers.handlers[current_engine] handler_class && handler_class.new(self) end
def mark_safe(value)
-
(SafeBuffer, Array
-)
Parameters:
-
the
(String, Array
) -- values to be marked safe.
def mark_safe(value) if value.respond_to? :map! value.map!{|v| v.html_safe if v } else value.html_safe if value end end
def yield_content(key, *args)
-
(String)
- Result HTML for the given +key+.
Parameters:
-
*args
() --
-
key
(Symbol
) --
def yield_content(key, *args) blocks = content_blocks[key.to_sym] return nil if blocks.empty? blocks.inject(SafeBuffer.new){ |all,content| all << capture_html(*args, &content) } end