handlebars-amd-rails 1.0.0.rc.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +10 -0
- data/README.md +33 -43
- data/handlebars-amd-rails.gemspec +2 -2
- data/lib/handlebars-amd-rails/rails/template.rb +11 -1
- data/lib/handlebars-amd-rails/version.rb +1 -1
- data/spec/fixtures/demo-with-partials.hbs +4 -0
- data/spec/template_spec.rb +40 -11
- metadata +18 -8
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Handlebars Amd Rails
|
2
2
|
|
3
|
-
Use Handlebars templates wrapped in a AMD define block, with the asset pipeline in Rails 3.1+ applications.
|
3
|
+
Use Handlebars templates precompiled and wrapped in a AMD define block, with the asset pipeline in Rails 3.1+ applications.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -32,28 +32,45 @@ Require handlebars.js in your AMD config file
|
|
32
32
|
|
33
33
|
Place individual Handlebars template file in their own file with template_name.{hbs, handlebars} extension.
|
34
34
|
|
35
|
+
```html
|
36
|
+
<!-- app/assets/javascripts/templates/shared/header.hbs -->
|
37
|
+
|
38
|
+
<header>
|
39
|
+
A logo perhaps?
|
40
|
+
</header>
|
35
41
|
```
|
36
|
-
/* app/assets/javascripts/templates/demo.hbs */
|
37
42
|
|
38
|
-
|
43
|
+
```html
|
44
|
+
<!-- app/assets/javascripts/templates/demo/index.hbs -->
|
45
|
+
|
46
|
+
{{>shared/_header}}
|
47
|
+
|
48
|
+
<p class="welcome">
|
49
|
+
Hello {{name}}! You have {{count}} new messages.
|
50
|
+
</p>
|
39
51
|
```
|
40
52
|
|
41
53
|
Which will be compiled and rendered as:
|
42
54
|
|
43
55
|
```javascript
|
44
|
-
define([
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
56
|
+
define([
|
57
|
+
"handlebars",
|
58
|
+
"partial!shared/_header"
|
59
|
+
], function(Handlebars) {
|
60
|
+
var templates = Handlebars.templates || (Handlebars.templates = {});
|
61
|
+
return templates['demo/index'] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
|
62
|
+
helpers = helpers || Handlebars.helpers; partials = partials || Handlebars.partials; data = data || {};
|
63
|
+
var buffer = "";
|
64
|
+
// …
|
65
|
+
return buffer;
|
66
|
+
});
|
50
67
|
});
|
51
68
|
```
|
52
69
|
|
53
70
|
You can take advantage of the asset pipeline by chaining your template through other processors like haml:
|
54
71
|
|
55
|
-
```
|
56
|
-
|
72
|
+
```haml
|
73
|
+
/ app/assets/javascripts/templates/demo/index.hbs.haml
|
57
74
|
|
58
75
|
%p.welcome
|
59
76
|
Hello {{name}}! You have {{count}} new messages.
|
@@ -62,9 +79,11 @@ You can take advantage of the asset pipeline by chaining your template through o
|
|
62
79
|
Which will be compiled and rendered as:
|
63
80
|
|
64
81
|
```javascript
|
65
|
-
define([
|
82
|
+
define([
|
83
|
+
"handlebars"
|
84
|
+
], function(Handlebars) {
|
66
85
|
var templates = Handlebars.templates || (Handlebars.templates = {});
|
67
|
-
return templates['demo'] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
|
86
|
+
return templates['demo/index'] = Handlebars.template(function (Handlebars,depth0,helpers,partials,data) {
|
68
87
|
helpers = helpers || Handlebars.helpers; data = data || {};
|
69
88
|
return "<p class='welcome'>Hello {{name}}! You have {{count}} new messages.</p>";
|
70
89
|
});
|
@@ -84,36 +103,7 @@ Using templates:
|
|
84
103
|
```javascript
|
85
104
|
require([
|
86
105
|
'jquery',
|
87
|
-
'template!demo'
|
88
|
-
], function ($, template) {
|
89
|
-
$('body').html(template({
|
90
|
-
name: 'Joe', count: 10
|
91
|
-
}))
|
92
|
-
});
|
93
|
-
```
|
94
|
-
|
95
|
-
If the template renders partials, partial files need to be required:
|
96
|
-
|
97
|
-
```
|
98
|
-
/* app/assets/javascripts/templates/demo.hbs */
|
99
|
-
|
100
|
-
{{>_header}}
|
101
|
-
|
102
|
-
Hello {{name}}! You have {{count}} new messages.
|
103
|
-
```
|
104
|
-
|
105
|
-
```
|
106
|
-
/* app/assets/javascripts/templates/_header.hbs */
|
107
|
-
|
108
|
-
%header
|
109
|
-
Hola!
|
110
|
-
```
|
111
|
-
|
112
|
-
```javascript
|
113
|
-
require([
|
114
|
-
'jquery',
|
115
|
-
'template!demo',
|
116
|
-
'partial!_header',
|
106
|
+
'template!demo/index'
|
117
107
|
], function ($, template) {
|
118
108
|
$('body').html(template({
|
119
109
|
name: 'Joe', count: 10
|
@@ -8,8 +8,8 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = Handlebars::Amd::Rails::VERSION
|
9
9
|
gem.authors = ["Steffen Leistner"]
|
10
10
|
gem.email = ["sleistner@gmail.com"]
|
11
|
-
gem.summary = %q{Use Handlebars templates wrapped in a AMD define block with rails.}
|
12
|
-
gem.description = %q{Use Handlebars templates wrapped in a AMD define block, with the asset pipeline in Rails 3.1+ applications.}
|
11
|
+
gem.summary = %q{Use Handlebars templates precompiled and wrapped in a AMD define block with rails.}
|
12
|
+
gem.description = %q{Use Handlebars templates precompiled and wrapped in a AMD define block, with the asset pipeline in Rails 3.1+ applications.}
|
13
13
|
gem.homepage = "https://github.com/sleistner/handlebars-amd-rails"
|
14
14
|
|
15
15
|
gem.rubyforge_project = "handlebars-amd-rails"
|
@@ -48,7 +48,7 @@ module Handlebars
|
|
48
48
|
|
49
49
|
def interpolate(template)
|
50
50
|
unindent <<-JS
|
51
|
-
define(
|
51
|
+
define(#{requirements}, function(Handlebars) {
|
52
52
|
var templates = Handlebars.templates || (Handlebars.templates = {});
|
53
53
|
return templates['#{template_name}'] = Handlebars.template(#{template});
|
54
54
|
});
|
@@ -62,6 +62,16 @@ module Handlebars
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
+
def requirements
|
66
|
+
JSON.pretty_generate(['handlebars'] + detect_partials)
|
67
|
+
end
|
68
|
+
|
69
|
+
def detect_partials
|
70
|
+
data.scan(/\{\{>\s*([^\s]*)\s*\}\}/).flatten.uniq.map do |partial|
|
71
|
+
"partial!#{partial}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
65
75
|
end
|
66
76
|
end
|
67
77
|
end
|
data/spec/template_spec.rb
CHANGED
@@ -2,9 +2,6 @@ require_relative './spec_helper'
|
|
2
2
|
|
3
3
|
describe Handlebars::Amd::Rails::Template do
|
4
4
|
|
5
|
-
subject do
|
6
|
-
Handlebars::Amd::Rails::Template.new(fixture_path('demo.hbs'))
|
7
|
-
end
|
8
5
|
|
9
6
|
describe '#evaluate' do
|
10
7
|
|
@@ -13,16 +10,48 @@ describe Handlebars::Amd::Rails::Template do
|
|
13
10
|
Handlebars::Amd::Rails::Source.stub(:path).and_return(helper('handlebars.js'))
|
14
11
|
end
|
15
12
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
13
|
+
context('without partials') do
|
14
|
+
|
15
|
+
subject do
|
16
|
+
Handlebars::Amd::Rails::Template.new(fixture_path('demo.hbs'))
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'compiles a template wrapped in a define block' do
|
20
|
+
expected = unindent <<-JS
|
21
|
+
define([
|
22
|
+
"handlebars"
|
23
|
+
], function(Handlebars) {
|
24
|
+
var templates = Handlebars.templates || (Handlebars.templates = {});
|
25
|
+
return templates['demo'] = Handlebars.template("Precompiled!");
|
26
|
+
});
|
27
|
+
JS
|
28
|
+
subject.evaluate(nil, nil).should == expected
|
24
29
|
end
|
25
30
|
|
31
|
+
describe('with partials') do
|
32
|
+
|
33
|
+
subject do
|
34
|
+
Handlebars::Amd::Rails::Template.new(fixture_path('demo-with-partials.hbs'))
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'compiles a template wrapped in a define block with partial requirements' do
|
38
|
+
expected = unindent <<-JS
|
39
|
+
define([
|
40
|
+
"handlebars",
|
41
|
+
"partial!shared/header",
|
42
|
+
"partial!contact/business_card",
|
43
|
+
"partial!shared/footer"
|
44
|
+
], function(Handlebars) {
|
45
|
+
var templates = Handlebars.templates || (Handlebars.templates = {});
|
46
|
+
return templates['demo-with-partials'] = Handlebars.template("Precompiled!");
|
47
|
+
});
|
48
|
+
JS
|
49
|
+
subject.evaluate(nil, nil).should == expected
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
26
55
|
end
|
27
56
|
|
28
57
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: handlebars-amd-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Steffen Leistner
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -59,8 +59,8 @@ dependencies:
|
|
59
59
|
- - ! '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
|
-
description: Use Handlebars templates wrapped in a AMD define block,
|
63
|
-
pipeline in Rails 3.1+ applications.
|
62
|
+
description: Use Handlebars templates precompiled and wrapped in a AMD define block,
|
63
|
+
with the asset pipeline in Rails 3.1+ applications.
|
64
64
|
email:
|
65
65
|
- sleistner@gmail.com
|
66
66
|
executables: []
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- .gitignore
|
71
71
|
- .jshintrc
|
72
72
|
- .rspec
|
73
|
+
- CHANGELOG.md
|
73
74
|
- Gemfile
|
74
75
|
- LICENSE.txt
|
75
76
|
- README.md
|
@@ -80,6 +81,7 @@ files:
|
|
80
81
|
- lib/handlebars-amd-rails/rails/railtie.rb
|
81
82
|
- lib/handlebars-amd-rails/rails/template.rb
|
82
83
|
- lib/handlebars-amd-rails/version.rb
|
84
|
+
- spec/fixtures/demo-with-partials.hbs
|
83
85
|
- spec/fixtures/demo.hbs
|
84
86
|
- spec/handlebars_spec.rb
|
85
87
|
- spec/helper/handlebars.js
|
@@ -101,19 +103,27 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
101
103
|
- - ! '>='
|
102
104
|
- !ruby/object:Gem::Version
|
103
105
|
version: '0'
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
hash: -1073423435357479313
|
104
109
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
110
|
none: false
|
106
111
|
requirements:
|
107
|
-
- - ! '
|
112
|
+
- - ! '>='
|
108
113
|
- !ruby/object:Gem::Version
|
109
|
-
version:
|
114
|
+
version: '0'
|
115
|
+
segments:
|
116
|
+
- 0
|
117
|
+
hash: -1073423435357479313
|
110
118
|
requirements: []
|
111
119
|
rubyforge_project: handlebars-amd-rails
|
112
120
|
rubygems_version: 1.8.23
|
113
121
|
signing_key:
|
114
122
|
specification_version: 3
|
115
|
-
summary: Use Handlebars templates wrapped in a AMD define block with
|
123
|
+
summary: Use Handlebars templates precompiled and wrapped in a AMD define block with
|
124
|
+
rails.
|
116
125
|
test_files:
|
126
|
+
- spec/fixtures/demo-with-partials.hbs
|
117
127
|
- spec/fixtures/demo.hbs
|
118
128
|
- spec/handlebars_spec.rb
|
119
129
|
- spec/helper/handlebars.js
|