hamlbars 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # master
2
2
 
3
+ # 1.1.0
4
+
5
+ * Add support for `:_action` attributes, and deprecate the `:event` and
6
+ `:events` syntax
7
+
8
+ # 1.0.0
9
+
3
10
  * Drop support for `.hbs` extension (#11)
4
11
 
5
12
  # 2012.3.21
data/README.md CHANGED
@@ -3,127 +3,215 @@
3
3
  [![Build Status](https://secure.travis-ci.org/jamesotron/hamlbars.png?branch=master)](http://travis-ci.org/jamesotron/hamlbars)
4
4
  [![Dependency Status](https://gemnasium.com/jamesotron/hamlbars.png)](https://gemnasium.com/jamesotron/hamlbars)
5
5
 
6
- [Hamlbars](https://github.com/jamesotron/hamlbars) is a Ruby gem which allows you to easily generate [Handlebar](http://handlebarsjs.com) templates using [Haml](http://www.haml-lang.com).
6
+ [Hamlbars](https://github.com/jamesotron/hamlbars) is a Ruby gem which allows
7
+ you to easily generate [Handlebars](http://handlebarsjs.com) templates using
8
+ [Haml](http://www.haml-lang.com).
9
+
10
+ # Installation
11
+
12
+ Add the following line to your Gemfile (on Rails, inside the `:assets` group):
13
+
14
+ ```ruby
15
+ gem 'hamlbars', '~> 1.1'
16
+ ```
17
+
18
+ If you are stuck with an older, yanked version like 2012.3.21 and it won't
19
+ update to 1.1, be sure to add `'~> 1.1'` as the version spec and run `bundle
20
+ install`.
7
21
 
8
22
  # Demo Site
9
23
 
10
- If you're unsure how all the pieces fit together then take a quick look at the [demo site](http://hamlbars-demo.herokuapp.com/).
24
+ If you're unsure how all the pieces fit together then take a quick look at the
25
+ [demo site](http://hamlbars-demo.herokuapp.com/).
11
26
 
12
27
  # Attribute bindings
13
28
 
14
- You can easily add attribute bindings by adding a `:bind` hash to the tag attributes, like so:
29
+ You can easily add attribute bindings by adding a `:bind` hash to the tag
30
+ attributes, like so:
15
31
 
16
- %div{ :class => 'widget', :bind => { :title => 'App.widgetController.title' }
32
+ ```haml
33
+ %div{ :class => 'widget', :bind => { :title => 'App.widgetController.title' }
34
+ ```
17
35
 
18
36
  Which will generate the following output:
19
37
 
20
- <div class="widget" {{bindAttr title="App.widgetController.title"}}></div>
38
+ ```handlebars
39
+ <div class="widget" {{bindAttr title="App.widgetController.title"}}></div>
40
+ ```
41
+
42
+ # Action handlers
43
+
44
+ To use Ember's `{{action}}` helper, set the `:_action` attribute, like so:
45
+
46
+ ```haml
47
+ %a{ :_action => 'toggle' } Toggle
48
+ %a{ :_action => 'edit article on="doubleClick"' } Edit
49
+ ```
50
+
51
+ This will generate:
52
+
53
+ ```html
54
+ <a {{action toggle}}>Toggle</a>
55
+ <a {{action edit article on="doubleClick"}}>Edit</a>
56
+ ```
57
+
58
+ Note that `:_action` has a leading underscore, to distinguish it from regular
59
+ HTML attributes (`<form action="...">`).
21
60
 
22
- # Event bindings
61
+ # Event bindings (old syntax)
23
62
 
24
- You can add one or more event actions by adding an event hash or array or event hashes to the tag options:
63
+ You can also add one or more event actions by adding an event hash or array of
64
+ event hashes to the tag options. This syntax is being deprecated in favor of
65
+ the newer `:_action` syntax described above.
25
66
 
26
- %a{ :event => { :on => 'click', :action => 'clicked' } } Click
67
+ ```haml
68
+ %a{ :event => { :on => 'click', :action => 'clicked' } } Click
69
+ ```
27
70
 
28
71
  or
29
72
 
30
- %div{ :events => [ { :on => 'mouseover', :action => 'highlightView' }, { :on => 'mouseout', :action => 'disableViewHighlight' } ] }
73
+ ```haml
74
+ %div{ :events => [ { :on => 'mouseover', :action => 'highlightView' }, { :on => 'mouseout', :action => 'disableViewHighlight' } ] }
75
+ ```
31
76
 
32
77
  Note that the default event is `click`, so it's not necessary to specify it:
33
78
 
34
- %a{ :event => { :action => 'clicked' } } Click
79
+ ```haml
80
+ %a{ :event => { :action => 'clicked' } } Click
81
+ ```
35
82
 
36
- # Handlebar helper
83
+ # Handlebars helper
37
84
 
38
- You can use the `handlebars` helper (or just `hb` for short) to generate both Handlebar blocks and expressions.
85
+ You can use the `handlebars` helper (or just `hb` for short) to generate both
86
+ Handlebars blocks and expressions.
39
87
 
40
88
  ## Expressions
41
89
 
42
- Generating Handlebars expressions is as simple as using the `handlebars` helper and providing the expression as a string argument:
90
+ Generating Handlebars expressions is as simple as using the `handlebars` helper
91
+ and providing the expression as a string argument:
43
92
 
44
- = hb 'App.widgetController.title'
93
+ ```haml
94
+ = hb 'App.widgetController.title'
95
+ ```
45
96
 
46
97
  which will will generate:
47
98
 
48
- {{App.widgetController.title}}
99
+ ```handlebars
100
+ {{App.widgetController.title}}
101
+ ```
49
102
 
50
103
  ## Blocks
51
104
 
52
- Whereas passing a block to the `handlebars` helper will create a Handlebars block expression:
105
+ Whereas passing a block to the `handlebars` helper will create a Handlebars
106
+ block expression:
53
107
 
54
- %ul.authors
55
- = hb 'each authors' do
56
- %li<
57
- = succeed ',' do
58
- = hb 'lastName'
59
- = hb 'firstName'
108
+ ```haml
109
+ %ul.authors
110
+ = hb 'each authors' do
111
+ %li<
112
+ = succeed ',' do
113
+ = hb 'lastName'
114
+ = hb 'firstName'
115
+ ```
60
116
 
61
117
  will result in the following markup:
62
118
 
63
- <ul class="authors">
64
- {{#each authors}}
65
- <li>{{lastName}}, {{firstName}}</li>
66
- {{/each}}
67
- </ul>
119
+ ```handlebars
120
+ <ul class="authors">
121
+ {{#each authors}}
122
+ <li>{{lastName}}, {{firstName}}</li>
123
+ {{/each}}
124
+ </ul>
125
+ ```
68
126
 
69
127
  ## Options
70
128
 
71
- The `hb` helper can take an optional hash of options which will be rendered inside the expression:
129
+ The `hb` helper can take an optional hash of options which will be rendered
130
+ inside the expression:
72
131
 
73
- = hb 'view App.InfoView', :tagName => 'span'
132
+ ```haml
133
+ = hb 'view App.InfoView', :tagName => 'span'
134
+ ```
74
135
 
75
136
  will result in:
76
137
 
77
- {{view App.InfoView tagName="span"}}
138
+ ```handlebars
139
+ {{view App.InfoView tagName="span"}}
140
+ ```
78
141
 
79
142
  ## Tripple-stash
80
143
 
81
- You can use the `handlebars!` or `hb!` variant of the `handlebars` helper to output "tripple-stash" expressions within which Handlebars does not escape the output.
144
+ You can use the `handlebars!` or `hb!` variant of the `handlebars` helper to
145
+ output "tripple-stash" expressions within which Handlebars does not escape the
146
+ output.
82
147
 
83
148
  # Configuring template output:
84
149
 
85
- `hamlbars` has three configuration options, which pertain to the generated JavaScript:
150
+ `hamlbars` has three configuration options, which pertain to the generated
151
+ JavaScript:
86
152
 
87
- Hamlbars::Template.template_destination # default 'Handlebars.templates'
88
- Hamlbars::Template.template_compiler # default 'Handlebars.compile'
89
- Hamlbars::Template.template_partial_method # default 'Handlebars.registerPartial'
153
+ ```ruby
154
+ Hamlbars::Template.template_destination # default 'Handlebars.templates'
155
+ Hamlbars::Template.template_compiler # default 'Handlebars.compile'
156
+ Hamlbars::Template.template_partial_method # default 'Handlebars.registerPartial'
157
+ ```
90
158
 
91
- These settings will work find by default if you are using Handlebars as a standalone JavaScript library, however if you are using something that embeds Handlebars within it then you'll have to change these.
159
+ These settings will work find by default if you are using Handlebars as a
160
+ standalone JavaScript library, however if you are using something that embeds
161
+ Handlebars within it then you'll have to change these.
92
162
 
93
163
  If you're using [Ember.js](http://www.emberjs.com) then you can use:
94
164
 
95
- Hamlbars::Template.render_templates_for :ember
165
+ ```ruby
166
+ Hamlbars::Template.render_templates_for :ember
167
+ ```
96
168
 
97
169
  Which is effectively the same as:
98
170
 
99
- Hamlbars::Template.template_destination = 'Ember.TEMPLATES'
100
- Hamlbars::Template.template_compiler = 'Ember.Handlebars.compile'
101
- Hamlbars::Template.template_partial_method = 'Ember.Handlebars.registerPartial'
171
+ ```ruby
172
+ Hamlbars::Template.template_destination = 'Ember.TEMPLATES'
173
+ Hamlbars::Template.template_compiler = 'Ember.Handlebars.compile'
174
+ Hamlbars::Template.template_partial_method = 'Ember.Handlebars.registerPartial'
175
+ ```
102
176
 
103
- The good news is that if you're using the [emberjs-rails](http://www.rubygems.org/gems/emberjs-rails) gem then it will automatically detect hamlbars and change it for you. Magic!
177
+ The good news is that if you're using the
178
+ [emberjs-rails](http://www.rubygems.org/gems/emberjs-rails) gem then it will
179
+ automatically detect hamlbars and change it for you. Magic!
104
180
 
105
- If you're using [ember-rails](http://rubygems.org/gems/ember-rails) then you'll need to put this in a initializer.
181
+ If you're using [ember-rails](http://rubygems.org/gems/ember-rails) then you'll
182
+ need to put this in a initializer.
106
183
 
107
184
  # Configuring JavaScript output:
108
185
 
109
- As of version 2012.3.21 `hamlbars` has experimental support for template precompilation using [ExecJS](http://rubygems.org/gems/execjs). If you want to enable this support you can use:
186
+ Hamlbars has experimental support for template precompilation using
187
+ [ExecJS](http://rubygems.org/gems/execjs). To enable it, call
110
188
 
111
- Hamlbars::Template.enable_precompiler!
189
+ ```ruby
190
+ Hamlbars::Template.enable_precompiler!
191
+ ```
112
192
 
113
193
  You can also disable enclosification (which is enabled by default) using:
114
194
 
115
- Hamlbars::Template.disable_closures!
195
+ ```ruby
196
+ Hamlbars::Template.disable_closures!
197
+ ```
116
198
 
117
199
  # Asset pipeline
118
200
 
119
- Hamlbars is specifically designed for use with Rails 3.1's asset pipeline. Simply create templates ending in `.js.hamlbars` and Sprockets will know what to do.
201
+ Hamlbars is specifically designed for use with Rails 3.1's asset pipeline.
202
+ Simply create templates ending in `.js.hamlbars` and Sprockets will know what
203
+ to do.
120
204
 
121
205
  # Rails helpers
122
206
 
123
- You can enable support by calling `Hamlbars::Template.enable_rails_helpers!`. Probably the best way to do this is to create an initializer. This is dangerous and possibly stupid as a large number of Rails' helpers require access to the request object, which is not present when compiling assets.
207
+ You can enable support by calling `Hamlbars::Template.enable_rails_helpers!`.
208
+ Probably the best way to do this is to create an initializer. This is
209
+ dangerous and possibly stupid as a large number of Rails' helpers require
210
+ access to the request object, which is not present when compiling assets.
124
211
 
125
212
  **Use at your own risk. You have been warned.**
126
213
 
127
214
  # License and Copyright.
128
215
 
129
- Hamlbars is Copyright &copy; 2012 [Sociable Limited](http://sociable.co.nz/) and licensed under the terms of the MIT License.
216
+ Hamlbars is Copyright &copy; 2012 [Sociable Limited](http://sociable.co.nz/)
217
+ and licensed under the terms of the MIT License.
@@ -1,44 +1,62 @@
1
1
  module Hamlbars
2
2
  module Ext
3
3
  module Compiler
4
- end
5
- end
6
- end
4
+ def self.handlebars_attributes(helper, attributes)
5
+ rendered_attributes = [].tap { |r|attributes.each { |k,v| r << "#{k}=\"#{v}\"" } }.join(' ')
6
+ " {{#{helper} #{rendered_attributes}}}"
7
+ end
7
8
 
8
- module Haml
9
- module Compiler
10
-
11
- class << self
12
- # Overload build_attributes in Haml::Compiler to allow
13
- # for the creation of handlebars bound attributes by
14
- # adding :bind hash to the tag attributes.
15
- def build_attributes_with_handlebars_attributes (is_html, attr_wrapper, escape_attrs, attributes={})
16
- attributes[:bind] = attributes.delete('bind') if attributes['bind']
17
- attributes[:event] = attributes.delete('event') if attributes['event']
18
- attributes[:events] = attributes.delete('events') if attributes['events']
19
- attributes[:events] ||= []
20
- attributes[:events] << attributes.delete(:event) if attributes[:event]
21
-
22
- handlebars_rendered_attributes = []
23
- handlebars_rendered_attributes << handlebars_attributes('bindAttr', attributes.delete(:bind)) if attributes[:bind]
24
- attributes[:events].each do |event|
25
- event[:on] = event.delete('on') || event.delete(:on) || 'click'
26
- action = event.delete('action') || event.delete(:action)
27
- handlebars_rendered_attributes << handlebars_attributes("action \"#{action}\"", event)
28
- end
29
- attributes.delete(:events)
9
+ def self.included(base)
10
+ base.instance_eval do
30
11
 
31
- (handlebars_rendered_attributes * '') +
32
- build_attributes_without_handlebars_attributes(is_html, attr_wrapper, escape_attrs, attributes)
33
- end
34
- alias build_attributes_without_handlebars_attributes build_attributes
35
- alias build_attributes build_attributes_with_handlebars_attributes
12
+ if Haml::VERSION >= "3.2"
13
+ # Haml 3.2 introduces hyphenate_data_attrs
14
+ def build_attributes_with_handlebars_attributes (is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes={})
15
+ handlebars_rendered_attributes = build_attributes_with_handlebars_attributes_base(is_html, attr_wrapper, escape_attrs, attributes)
36
16
 
37
- private
17
+ (handlebars_rendered_attributes * '') +
18
+ build_attributes_without_handlebars_attributes(is_html, attr_wrapper, escape_attrs, hyphenate_data_attrs, attributes)
19
+ end
20
+ else
21
+ def build_attributes_with_handlebars_attributes (is_html, attr_wrapper, escape_attrs, attributes={})
22
+ handlebars_rendered_attributes = build_attributes_with_handlebars_attributes_base(is_html, attr_wrapper, escape_attrs, attributes)
38
23
 
39
- def handlebars_attributes(helper, attributes)
40
- rendered_attributes = [].tap { |r|attributes.each { |k,v| r << "#{k}=\"#{v}\"" } }.join(' ')
41
- " {{#{helper} #{rendered_attributes}}}"
24
+ (handlebars_rendered_attributes * '') +
25
+ build_attributes_without_handlebars_attributes(is_html, attr_wrapper, escape_attrs, attributes)
26
+ end
27
+ end
28
+
29
+ # Overload build_attributes in Haml::Compiler to allow
30
+ # for the creation of handlebars bound attributes by
31
+ # adding :bind hash to the tag attributes.
32
+ def build_attributes_with_handlebars_attributes_base(is_html, attr_wrapper, escape_attrs, attributes={})
33
+ handlebars_rendered_attributes = []
34
+
35
+ if bind = attributes.delete('bind')
36
+ handlebars_rendered_attributes << Hamlbars::Ext::Compiler.handlebars_attributes('bindAttr', bind)
37
+ end
38
+
39
+ events = attributes.delete('events') || []
40
+ if event = attributes.delete('event')
41
+ events << event
42
+ end
43
+ events.each do |event|
44
+ event[:on] = event.delete('on') || event.delete(:on) || 'click'
45
+ action = event.delete('action') || event.delete(:action)
46
+ handlebars_rendered_attributes << Hamlbars::Ext::Compiler.handlebars_attributes("action \"#{action}\"", event)
47
+ end
48
+
49
+ # This could be generalized into /_.*/ catch-all syntax, if
50
+ # necessary. https://github.com/jamesotron/hamlbars/pull/33
51
+ if action = attributes.delete('_action')
52
+ handlebars_rendered_attributes << " {{action #{action}}}"
53
+ end
54
+
55
+ handlebars_rendered_attributes
56
+ end
57
+ alias build_attributes_without_handlebars_attributes build_attributes
58
+ alias build_attributes build_attributes_with_handlebars_attributes
59
+ end
42
60
  end
43
61
  end
44
62
  end
@@ -1,3 +1,3 @@
1
1
  module Hamlbars
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hamlbars
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-28 00:00:00.000000000 Z
12
+ date: 2012-08-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: haml
@@ -98,7 +98,7 @@ dependencies:
98
98
  requirements:
99
99
  - - ! '>='
100
100
  - !ruby/object:Gem::Version
101
- version: '0'
101
+ version: 2.10.0
102
102
  type: :development
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
@@ -106,7 +106,7 @@ dependencies:
106
106
  requirements:
107
107
  - - ! '>='
108
108
  - !ruby/object:Gem::Version
109
- version: '0'
109
+ version: 2.10.0
110
110
  - !ruby/object:Gem::Dependency
111
111
  name: activesupport
112
112
  requirement: !ruby/object:Gem::Requirement
@@ -156,15 +156,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
156
156
  - - ! '>='
157
157
  - !ruby/object:Gem::Version
158
158
  version: '0'
159
+ segments:
160
+ - 0
161
+ hash: -951484081994800646
159
162
  required_rubygems_version: !ruby/object:Gem::Requirement
160
163
  none: false
161
164
  requirements:
162
165
  - - ! '>='
163
166
  - !ruby/object:Gem::Version
164
167
  version: '0'
168
+ segments:
169
+ - 0
170
+ hash: -951484081994800646
165
171
  requirements: []
166
172
  rubyforge_project:
167
- rubygems_version: 1.8.19
173
+ rubygems_version: 1.8.24
168
174
  signing_key:
169
175
  specification_version: 3
170
176
  summary: Extensions to Haml to allow creation of handlebars expressions.