Class: Forme::ERB::Form

Inherits:
Form
  • Object
show all
Defined in:
lib/forme/erb_form.rb

Overview

Subclass used when using Forme ERB integration. Handles integrating into the view template so that methods with blocks can inject strings into the output.

Instance Attribute Summary collapse

Attributes inherited from Form

#hidden_tags, #input_defaults, #opts, #serializer

Instance Method Summary collapse

Methods inherited from Form

#<<, #_input, #_inputs, #_tag, #button, #close, #each_obj, form, #input, #namespaces, new, #obj, #open, #raw, #raw_output, #tag_, #with_obj, #with_opts

Constructor Details

#initializeForm

Set the template output object when initializing.



16
17
18
19
# File 'lib/forme/erb_form.rb', line 16

def initialize(*)
  super
  @output = @opts[:output] ? @opts[:output] : String.new
end

Instance Attribute Details

#outputObject (readonly)

Template output object, where serialized output gets injected.



13
14
15
# File 'lib/forme/erb_form.rb', line 13

def output
  @output
end

Instance Method Details

#capture(block = String.new) ⇒ Object

:nodoc:



64
65
66
67
68
69
70
# File 'lib/forme/erb_form.rb', line 64

def capture(block=String.new) # :nodoc:
  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.



22
23
24
# File 'lib/forme/erb_form.rb', line 22

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.



38
39
40
41
42
43
44
# File 'lib/forme/erb_form.rb', line 38

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.



28
29
30
31
32
33
34
# File 'lib/forme/erb_form.rb', line 28

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.



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/forme/erb_form.rb', line 50

def tag(type, attr={}, children=[], &block)
  tag = _tag(type, attr, children)
  if block
    capture(block) do
      emit(serialize_open(tag))
      Array(tag.children).each{|c| emit(c)}
      yield self
      emit(serialize_close(tag))
    end
  else
    tag
  end
end