Module: TagHelper

Defined in:
lib/tag_helper.rb

Overview

XHTML tags builder

Class Method Summary collapse

Class Method Details

.attributes(hash) ⇒ Object



21
22
23
24
25
# File 'lib/tag_helper.rb', line 21

def attributes(hash)
  hash.to_a
    .reject { |_k, v| v.nil? }
    .map { |k, v| %(#{escape_html(k)}="#{escape_html(v)}") }.join(' ')
end

.content(tag, value, attrs = {}) ⇒ Object



11
12
13
14
15
# File 'lib/tag_helper.rb', line 11

def content(tag, value, attrs = {})
  start_tag = "<#{tag_and_attributes(tag, attributes(attrs))}>"
  end_tag   = "</#{tag}>"
  [start_tag, escape_html(value), end_tag].join
end

.escape_html(str) ⇒ Object



27
28
29
# File 'lib/tag_helper.rb', line 27

def escape_html(str)
  CGI.escapeHTML(str.to_s)
end

.tag_and_attributes(tag, attributes) ⇒ Object



17
18
19
# File 'lib/tag_helper.rb', line 17

def tag_and_attributes(tag, attributes)
  attributes.empty? ? tag : "#{tag} #{attributes}"
end

.unary(tag, attrs = {}) ⇒ Object



7
8
9
# File 'lib/tag_helper.rb', line 7

def unary(tag, attrs = {})
  "<#{tag_and_attributes(tag, attributes(attrs))} />"
end