Module: MarkupHelpers

Includes:
Haml::Helpers, TiltHelpers
Defined in:
lib/markup_helpers.rb,
lib/markup_helpers/haml.rb,
lib/markup_helpers/tilt.rb,
lib/markup_helpers/version.rb

Defined Under Namespace

Modules: TiltHelpers

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Methods included from TiltHelpers

#capture, #concat

Instance Method Details

#tag(tag_name, content_or_attrs = {}, attrs = nil, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/markup_helpers.rb', line 7

def tag tag_name, content_or_attrs = {}, attrs = nil, &block
  if attrs.nil? && Hash === content_or_attrs
    attrs   = content_or_attrs 
    content = nil
  else
    attrs ||= {}
    content = content_or_attrs
  end

  attrs = attrs.map do |name, val|
    next if val.nil? || val == []
    val == true ? name : %{#{name}="#{[*val].join(' ')}"}
  end.compact.join(' ')

  attrs   = " #{attrs}" unless attrs.empty?
  if block_given?
    content = respond_to?(:capture) ? capture(&block) : yield 
  end
  output  = %{<#{tag_name}#{attrs}>#{content}</#{tag_name}>}
  block_given? && respond_to?(:concat) ? concat(output) : output
end