Module: NForm::HTML
- Included in:
- Builder
- Defined in:
- lib/nform/html.rb
Overview
Helper Methods for generating HTML markup
Constant Summary collapse
- VOID_ELEMENTS =
%i|area base br col embed hr img input keygen link meta param source track wbr|
- BOOL_ATTRIBUTES =
%i|allowfullscreen async autofocus autoplay checked compact controls declare default defaultchecked defaultmuted defaultselected defer disabled draggable enabled formnovalidate hidden indeterminate inert ismap itemscope loop multiple muted nohref noresize noshade novalidate nowrap open pauseonexit readonly required reversed scoped seamless selected sortable spellcheck translate truespeed typemustmatch visible|
Instance Method Summary collapse
- #attr_key(k) ⇒ Object
- #attr_string(k, v) ⇒ Object
- #attrs(hash = {}) ⇒ Object
- #njoin(*args) ⇒ Object
- #sjoin(*args) ⇒ Object
-
#tag(name, attributes = {}, &block) ⇒ Object
Generate an HTML Tag.
- #zjoin(*args) ⇒ Object
Instance Method Details
#attr_key(k) ⇒ Object
34 35 36 |
# File 'lib/nform/html.rb', line 34 def attr_key(k) k.is_a?(Symbol) ? k.to_s.gsub("_","-") : k end |
#attr_string(k, v) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/nform/html.rb', line 26 def attr_string(k,v) if BOOL_ATTRIBUTES.include?(k) attr_key(k) if v else %Q|#{attr_key(k)}="#{v}"| end end |
#attrs(hash = {}) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/nform/html.rb', line 19 def attrs(hash={}) hash.delete_if{|k,v| v.nil? || v == "" } .map{|k,v| attr_string(k,v) } .compact .join(" ") end |
#njoin(*args) ⇒ Object
46 47 48 |
# File 'lib/nform/html.rb', line 46 def njoin(*args) args.delete_if{|a| a.nil? || a == ""}.join("\n") end |
#sjoin(*args) ⇒ Object
42 43 44 |
# File 'lib/nform/html.rb', line 42 def sjoin(*args) args.delete_if{|a| a.nil? || a == ""}.join(' ') end |
#tag(name, attributes = {}, &block) ⇒ Object
Generate an HTML Tag
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/nform/html.rb', line 8 def tag(name, attributes={}, &block) open = sjoin name, attrs(attributes) body = block.call if block_given? if VOID_ELEMENTS.include?(name.to_sym) raise BuilderError, "Void elements cannot have content" if body "<#{open}>" else "<#{open}>#{body}</#{name}>" end end |
#zjoin(*args) ⇒ Object
38 39 40 |
# File 'lib/nform/html.rb', line 38 def zjoin(*args) args.delete_if{|a| a.nil? || a == ""}.join('') end |