hamlbars 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.md +7 -0
- data/README.md +136 -48
- data/lib/hamlbars/ext/compiler.rb +52 -34
- data/lib/hamlbars/version.rb +1 -1
- metadata +11 -5
data/History.md
CHANGED
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
|
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
|
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
|
29
|
+
You can easily add attribute bindings by adding a `:bind` hash to the tag
|
30
|
+
attributes, like so:
|
15
31
|
|
16
|
-
|
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
|
-
|
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
|
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
|
-
|
67
|
+
```haml
|
68
|
+
%a{ :event => { :on => 'click', :action => 'clicked' } } Click
|
69
|
+
```
|
27
70
|
|
28
71
|
or
|
29
72
|
|
30
|
-
|
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
|
-
|
79
|
+
```haml
|
80
|
+
%a{ :event => { :action => 'clicked' } } Click
|
81
|
+
```
|
35
82
|
|
36
|
-
#
|
83
|
+
# Handlebars helper
|
37
84
|
|
38
|
-
You can use the `handlebars` helper (or just `hb` for short) to generate both
|
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
|
90
|
+
Generating Handlebars expressions is as simple as using the `handlebars` helper
|
91
|
+
and providing the expression as a string argument:
|
43
92
|
|
44
|
-
|
93
|
+
```haml
|
94
|
+
= hb 'App.widgetController.title'
|
95
|
+
```
|
45
96
|
|
46
97
|
which will will generate:
|
47
98
|
|
48
|
-
|
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
|
105
|
+
Whereas passing a block to the `handlebars` helper will create a Handlebars
|
106
|
+
block expression:
|
53
107
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
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
|
129
|
+
The `hb` helper can take an optional hash of options which will be rendered
|
130
|
+
inside the expression:
|
72
131
|
|
73
|
-
|
132
|
+
```haml
|
133
|
+
= hb 'view App.InfoView', :tagName => 'span'
|
134
|
+
```
|
74
135
|
|
75
136
|
will result in:
|
76
137
|
|
77
|
-
|
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
|
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
|
150
|
+
`hamlbars` has three configuration options, which pertain to the generated
|
151
|
+
JavaScript:
|
86
152
|
|
87
|
-
|
88
|
-
|
89
|
-
|
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
|
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
|
-
|
165
|
+
```ruby
|
166
|
+
Hamlbars::Template.render_templates_for :ember
|
167
|
+
```
|
96
168
|
|
97
169
|
Which is effectively the same as:
|
98
170
|
|
99
|
-
|
100
|
-
|
101
|
-
|
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
|
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
|
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
|
-
|
186
|
+
Hamlbars has experimental support for template precompilation using
|
187
|
+
[ExecJS](http://rubygems.org/gems/execjs). To enable it, call
|
110
188
|
|
111
|
-
|
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
|
-
|
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.
|
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!`.
|
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 © 2012 [Sociable Limited](http://sociable.co.nz/)
|
216
|
+
Hamlbars is Copyright © 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
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
9
|
-
|
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
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
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
|
data/lib/hamlbars/version.rb
CHANGED
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.
|
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-
|
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:
|
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:
|
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.
|
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.
|