Module: Hamlbars::Ext::Compiler

Defined in:
lib/hamlbars/ext/compiler.rb

Class Method Summary collapse

Class Method Details

.handlebars_attributes(helper, attributes) ⇒ Object



4
5
6
7
# File 'lib/hamlbars/ext/compiler.rb', line 4

def self.handlebars_attributes(helper, attributes)
  rendered_attributes = [].tap { |r|attributes.each { |k,v| r << "#{k}=\"#{v}\"" } }.join(' ')
  " {{#{helper} #{rendered_attributes}}}"
end

.included(base) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hamlbars/ext/compiler.rb', line 9

def self.included(base)
  base.instance_eval do

    if Haml::VERSION >= "3.2"
      # Haml 3.2 introduces hyphenate_data_attrs
      def build_attributes_with_handlebars_attributes (is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes={})
        handlebars_rendered_attributes = build_attributes_with_handlebars_attributes_base(is_html, attr_wrapper, escape_attrs, attributes)

        (handlebars_rendered_attributes * '') +
          build_attributes_without_handlebars_attributes(is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes)
      end
    else
      def build_attributes_with_handlebars_attributes (is_html, attr_wrapper, escape_attrs, attributes={})
        handlebars_rendered_attributes = build_attributes_with_handlebars_attributes_base(is_html, attr_wrapper, escape_attrs, attributes)

        (handlebars_rendered_attributes * '') +
          build_attributes_without_handlebars_attributes(is_html, attr_wrapper, escape_attrs, attributes)
      end
    end

    # Overload build_attributes in Haml::Compiler to allow
    # for the creation of handlebars bound attributes by
    # adding :bind hash to the tag attributes.
    def build_attributes_with_handlebars_attributes_base(is_html, attr_wrapper, escape_attrs, attributes={})
      handlebars_rendered_attributes = []

      if bind = attributes.delete('bind')
        handlebars_rendered_attributes << Hamlbars::Ext::Compiler.handlebars_attributes('bindAttr', bind)
      end

      events = attributes.delete('events') || []
      if event = attributes.delete('event')
        events << event
      end
      events.each do |event|
        event[:on] = event.delete('on') || event.delete(:on) || 'click'
        action = event.delete('action') || event.delete(:action)
        handlebars_rendered_attributes << Hamlbars::Ext::Compiler.handlebars_attributes("action \"#{action}\"", event)
      end

      # This could be generalized into /_.*/ catch-all syntax, if
      # necessary. https://github.com/jamesotron/hamlbars/pull/33
      if action = attributes.delete('_action')
        handlebars_rendered_attributes << " {{action #{action}}}"
      end

      handlebars_rendered_attributes
    end
    alias build_attributes_without_handlebars_attributes build_attributes
    alias build_attributes build_attributes_with_handlebars_attributes
  end
end