Module: HamlHelper

Included in:
Amber::Render::View
Defined in:
lib/amber/render/helpers/haml_helper.rb

Instance Method Summary collapse

Instance Method Details

#content_tag(name, content_or_options_with_block = nil, options = nil, &block) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/amber/render/helpers/haml_helper.rb', line 54

def (name, content_or_options_with_block = nil, options = nil, &block)
  if block_given?
    options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
    capture_haml { haml(name, options, &block) }
  else
    capture_haml { haml_tag(name, content_or_options_with_block, options) }
  end
end

#haml(name = nil, *args, &block) ⇒ Object

acts like haml_tag, capture_haml, or haml_concat, depending on how it is called.

two or more args –> like haml_tag one arg and a block –> like haml_tag zero args and a block –> like capture_haml one arg and no block –> like haml_concat

additionally, we allow the use of more than one class.

some examples of these usages:

def display_robot(robot)
  haml do                                # like capture_haml
    haml '.head', robot.head_html        # like haml_tag
    haml '.head' do                      # same
      haml robot.head_html
    end
    haml '.body.metal', robot.body_html  # like haml_tag, but with multiple classes
    haml '<a href="/x">link</a>'         # like haml_concat
  end
end

wrapping the helper in a capture_haml call is very useful, because then the helper can be used wherever a normal helper would be.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/amber/render/helpers/haml_helper.rb', line 31

def haml(name=nil, *args, &block)
  if name
    if args.empty? and block.nil?
      haml_concat name
    else
      if name =~ /^(.*?\.[^\.]*)(\..*)$/
        # allow chaining of classes if there are multiple '.' in the first arg
        name = $1
        classes = $2.gsub('.',' ')
        hsh = args.detect{|i| i.is_a?(Hash)}
        unless hsh
          hsh = {}
          args << hsh
        end
        hsh[:class] = classes
      end
      haml_tag(name, *args, &block)
    end
  else
    capture_haml(&block)
  end
end