Module: Padrino::Helpers::OutputHelpers
- Defined in:
- lib/padrino-helpers/output_helpers.rb,
lib/padrino-helpers/output_helpers/erb_handler.rb,
lib/padrino-helpers/output_helpers/haml_handler.rb,
lib/padrino-helpers/output_helpers/slim_handler.rb,
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, HamlitHandler, SlimHandler
Class Method Summary collapse
-
.handlers ⇒ Object
Returns the list of all available template handlers.
- .included(base) ⇒ Object
-
.register(engine, handler) ⇒ Object
Registers a new handler as available to the output helpers.
Instance Method Summary collapse
-
#block_is_template?(block) ⇒ Boolean
Returns true if the block is from a supported template type; false otherwise.
-
#capture_html(*args, &block) ⇒ String
(also: #capture)
Captures the html from a block of template code for any available handler.
-
#concat_content(text = "") ⇒ Object
(also: #concat)
Outputs the given text to the templates buffer directly.
-
#concat_safe_content(text = "") ⇒ Object
Outputs the given text to the templates buffer directly, assuming that it is safe.
-
#content_blocks ⇒ Object
protected
Retrieves content_blocks stored by content_for or within yield_content.
-
#content_for(key, content = nil, options = {}, &block) ⇒ Object
Capture a block or text of content to be rendered at a later time.
-
#content_for?(key) ⇒ TrueClass, FalseClass
Is there a content block for a given key?.
-
#find_proper_handler ⇒ Object
protected
Retrieves the template handler for the given output context.
-
#mark_safe(value) ⇒ SafeBuffer+
protected
Marks a String or a collection of Strings as safe.
-
#yield_content(key, *args) ⇒ String
Render the captured content blocks for a given key.
Class Method Details
.handlers ⇒ Object
Returns the list of all available template handlers.
14 15 16 |
# File 'lib/padrino-helpers/output_helpers.rb', line 14 def self.handlers @_template_handlers ||= {} end |
.included(base) ⇒ Object
7 8 9 |
# File 'lib/padrino-helpers/output_helpers.rb', line 7 def self.included(base) base.send(:include, SinatraCurrentEngine) unless base.method_defined?(:current_engine) end |
.register(engine, handler) ⇒ Object
Registers a new handler as available to the output helpers.
21 22 23 |
# File 'lib/padrino-helpers/output_helpers.rb', line 21 def self.register(engine, handler) handlers[engine] = 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.
116 117 118 119 |
# File 'lib/padrino-helpers/output_helpers.rb', line 116 def block_is_template?(block) handler = find_proper_handler block && handler && handler.engine_matches?(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.
61 62 63 64 65 66 67 |
# File 'lib/padrino-helpers/output_helpers.rb', line 61 def capture_html(*args, &block) if handler = find_proper_handler handler.capture_from_template(*args, &block) else yield(*args) end 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.
81 82 83 84 85 86 87 |
# File 'lib/padrino-helpers/output_helpers.rb', line 81 def concat_content(text="") if handler = find_proper_handler handler.concat_to_template(text, binding) else text end end |
#concat_safe_content(text = "") ⇒ Object
Outputs the given text to the templates buffer directly, assuming that it is safe.
100 101 102 |
# File 'lib/padrino-helpers/output_helpers.rb', line 100 def concat_safe_content(text="") concat_content text.html_safe end |
#content_blocks ⇒ Object (protected)
Retrieves content_blocks stored by content_for or within yield_content.
194 195 196 |
# File 'lib/padrino-helpers/output_helpers.rb', line 194 def content_blocks @content_blocks ||= Hash.new { |h,k| h[k] = [] } 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
.
143 144 145 146 147 |
# File 'lib/padrino-helpers/output_helpers.rb', line 143 def content_for(key, content = nil, = {}, &block) = content if content.is_a?(Hash) content_blocks[key.to_sym].clear if [:flush] 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?
160 161 162 |
# File 'lib/padrino-helpers/output_helpers.rb', line 160 def content_for?(key) !content_blocks[key.to_sym].empty? end |
#find_proper_handler ⇒ Object (protected)
Retrieves the template handler for the given output context. Can handle any output related to capturing or concatenating in a given template.
205 206 207 208 |
# File 'lib/padrino-helpers/output_helpers.rb', line 205 def find_proper_handler handler_class = OutputHelpers.handlers[current_engine] handler_class && handler_class.new(self) end |
#mark_safe(value) ⇒ SafeBuffer+ (protected)
Marks a String or a collection of Strings as safe. ‘nil` is accepted but ignored.
217 218 219 220 221 222 223 |
# File 'lib/padrino-helpers/output_helpers.rb', line 217 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 |
#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.
181 182 183 184 185 |
# File 'lib/padrino-helpers/output_helpers.rb', line 181 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 |