module NForm::HTML

Helper Methods for generating HTML markup

Constants

BOOL_ATTRIBUTES
VOID_ELEMENTS

Public Instance Methods

attr_key(k) click to toggle source
# 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) click to toggle source
# 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={}) click to toggle source
# 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) click to toggle source
# File lib/nform/html.rb, line 46
def njoin(*args)
  args.delete_if{|a| a.nil? || a == ""}.join("\n")
end
sjoin(*args) click to toggle source
# File lib/nform/html.rb, line 42
def sjoin(*args)
  args.delete_if{|a| a.nil? || a == ""}.join(' ')
end
tag(name, attributes={}, &block) click to toggle source

Generate an HTML Tag

# 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) click to toggle source
# File lib/nform/html.rb, line 38
def zjoin(*args)
  args.delete_if{|a| a.nil? || a == ""}.join('')
end