Module: Munge::Helper::Tag

Defined in:
lib/munge/helper/tag.rb

Instance Method Summary collapse

Instance Method Details

#content_tag(name, content = nil, options = {}, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/munge/helper/tag.rb', line 14

def (name, content = nil, options = {}, &block)
  if content.is_a?(Hash)
    options = content
    content = nil
  end

  html_attributes =
    if options.any?
      " " + options.map { |k, v| %(#{k}="#{h(v)}") }.join(" ")
    end

  inner_html =
    if content
      content
    elsif block_given?
      capture(&block)
    end

  "<#{name}#{html_attributes}>#{inner_html}</#{name}>"
end

#empty_tag(name, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/munge/helper/tag.rb', line 4

def empty_tag(name, options = {})
  options_str = options.map { |k, v| %(#{k}="#{h(v)}") }.join(" ")

  if options_str == ""
    "<#{name} />"
  else
    "<#{name} #{options_str} />"
  end
end

#h(string) ⇒ Object



35
36
37
# File 'lib/munge/helper/tag.rb', line 35

def h(string)
  CGI.escape_html(string.to_s)
end