Class: Forme::Sinatra::Form
Overview
Subclass used when using Forme/Sinatra ERB integration. Handles integrating into the view template so that methods with blocks can inject strings into the output.
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Template output object, where serialized output gets injected.
Attributes inherited from Form
#error_handler, #formatter, #hidden_tags, #input_defaults, #inputs_wrapper, #labeler, #obj, #opts, #serializer, #wrapper
Instance Method Summary collapse
- #capture(block = '') ⇒ Object
-
#emit(tag) ⇒ Object
Serialize the tag and inject it into the output.
-
#form(*a, &block) ⇒ Object
Capture the inside of the form, injecting it into the template if a block is given, or returning it as a string if not.
-
#initialize ⇒ Form
constructor
Set the template output object when initializing.
-
#inputs(*a, &block) ⇒ Object
Capture the inside of the inputs, injecting it into the template if a block is given, or returning it as a string if not.
-
#tag(type, attr = {}, children = [], &block) ⇒ Object
If a block is given, inject an opening tag into the output, inject any given children into the output, yield to the block, inject a closing tag into the output.
Methods inherited from Form
#<<, #_input, #_inputs, #_tag, #button, #close, form, #format, #input, #open, #serialize, #tag_, #transform, #transformer
Constructor Details
#initialize ⇒ Form
Set the template output object when initializing.
31 32 33 34 |
# File 'lib/forme/sinatra.rb', line 31 def initialize(*) super @output = @opts[:output] ? @opts[:output] : '' end |
Instance Attribute Details
#output ⇒ Object (readonly)
Template output object, where serialized output gets injected.
28 29 30 |
# File 'lib/forme/sinatra.rb', line 28 def output @output end |
Instance Method Details
#capture(block = '') ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/forme/sinatra.rb', line 79 def capture(block='') buf_was, @output = @output, block.is_a?(Proc) ? (eval("@_out_buf", block.binding) || @output) : block yield ret = @output @output = buf_was ret end |
#emit(tag) ⇒ Object
Serialize the tag and inject it into the output.
37 38 39 |
# File 'lib/forme/sinatra.rb', line 37 def emit(tag) output << tag.to_s end |
#form(*a, &block) ⇒ Object
Capture the inside of the form, injecting it into the template if a block is given, or returning it as a string if not.
53 54 55 56 57 58 59 |
# File 'lib/forme/sinatra.rb', line 53 def form(*a, &block) if block capture(block){super} else super end end |
#inputs(*a, &block) ⇒ Object
Capture the inside of the inputs, injecting it into the template if a block is given, or returning it as a string if not.
43 44 45 46 47 48 49 |
# File 'lib/forme/sinatra.rb', line 43 def inputs(*a, &block) if block capture(block){super} else capture{super} end end |
#tag(type, attr = {}, children = [], &block) ⇒ Object
If a block is given, inject an opening tag into the output, inject any given children into the output, yield to the block, inject a closing tag into the output. If a block is not given, just return the tag created.
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/forme/sinatra.rb', line 65 def tag(type, attr={}, children=[], &block) tag = _tag(type, attr, children) if block capture(block) do emit(serializer.serialize_open(tag)) if serializer.respond_to?(:serialize_open) Array(tag.children).each{|c| emit(c)} yield self emit(serializer.serialize_close(tag)) if serializer.respond_to?(:serialize_close) end else tag end end |