class Formular::HtmlBlock
the HtmlBlock
class is responsible for converting an element into an html string using the provided function providing the element as a variable to that function. this class also provides some simple helpers to make it easier to define your html.
Attributes
Public Class Methods
# File lib/formular/html_block.rb, line 6 def initialize(element, fn) @fn = fn @element = element @context = get_context end
Public Instance Methods
this calls our html function passing the element instance as a variable. It returns our html as a string
# File lib/formular/html_block.rb, line 15 def call @output = '' instance_exec(element, &fn).to_s end
return a closed start tag (e.g. <input name=“body”/>)
# File lib/formular/html_block.rb, line 37 def closed_start_tag start_tag.gsub(/\>$/, '/>') end
append a string to the output buffer. Useful when your html block is a bit more than one line
# File lib/formular/html_block.rb, line 22 def concat(content) @output << content.to_s end
returns the end/ closing tag for an element
# File lib/formular/html_block.rb, line 42 def end_tag "</#{element.tag}>" end
return the start/opening tag with the element attributes hash converted into valid html attributes
# File lib/formular/html_block.rb, line 28 def start_tag if element.attributes.empty? "<#{element.tag}>" else "<#{element.tag} #{element.attributes.to_html}>" end end
Private Instance Methods
# File lib/formular/html_block.rb, line 52 def get_context fn.binding.receiver end
Forward missing methods to the current context. This allows to access views local variables from nested content blocks.
@since 0.1.0 @api private
# File lib/formular/html_block.rb, line 66 def method_missing(m, *args, &blk) @context.__send__(m, *args, &blk) end