templebars 0.0.6 → 0.0.7

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/Gemfile.lock CHANGED
@@ -1,64 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- templebars (0.0.5)
4
+ templebars (0.0.6)
5
5
  execjs
6
- railties (>= 3.2.0)
6
+ sprockets (>= 2.0.3)
7
+ tilt
7
8
 
8
9
  GEM
9
10
  remote: http://rubygems.org/
10
11
  specs:
11
- actionpack (3.2.1)
12
- activemodel (= 3.2.1)
13
- activesupport (= 3.2.1)
14
- builder (~> 3.0.0)
15
- erubis (~> 2.7.0)
16
- journey (~> 1.0.1)
17
- rack (~> 1.4.0)
18
- rack-cache (~> 1.1)
19
- rack-test (~> 0.6.1)
20
- sprockets (~> 2.1.2)
21
- activemodel (3.2.1)
22
- activesupport (= 3.2.1)
23
- builder (~> 3.0.0)
24
- activesupport (3.2.1)
25
- i18n (~> 0.6)
26
- multi_json (~> 1.0)
27
- builder (3.0.0)
28
- erubis (2.7.0)
29
- execjs (1.3.0)
12
+ execjs (1.3.1)
30
13
  multi_json (~> 1.0)
31
14
  hike (1.2.1)
32
- i18n (0.6.0)
33
- journey (1.0.3)
34
- json (1.6.6)
35
- multi_json (1.2.0)
15
+ multi_json (1.3.6)
36
16
  rack (1.4.1)
37
- rack-cache (1.2)
38
- rack (>= 0.4)
39
- rack-ssl (1.3.2)
40
- rack
41
- rack-test (0.6.1)
42
- rack (>= 1.0)
43
- railties (3.2.1)
44
- actionpack (= 3.2.1)
45
- activesupport (= 3.2.1)
46
- rack-ssl (~> 1.3.2)
47
- rake (>= 0.8.7)
48
- rdoc (~> 3.4)
49
- thor (~> 0.14.6)
50
17
  rake (0.9.2.2)
51
- rdoc (3.12)
52
- json (~> 1.4)
53
- sprockets (2.1.2)
18
+ sprockets (2.1.3)
54
19
  hike (~> 1.2)
55
20
  rack (~> 1.0)
56
21
  tilt (~> 1.1, != 1.3.0)
57
- thor (0.14.6)
58
22
  tilt (1.3.3)
59
23
 
60
24
  PLATFORMS
61
25
  ruby
62
26
 
63
27
  DEPENDENCIES
28
+ rake
64
29
  templebars!
data/README.md CHANGED
@@ -1,37 +1,66 @@
1
1
  # templebars
2
2
 
3
- `templebars` allows you to precomile Handlebars templates and make them available globally in a `Templates` object. (This can be set via `Templebars::Rails::GLOBAL`) Create files with a "handlebars", "hbs", or "hb" extension inside of a `templates/` directory in any `assets/javascripts/` directory:
3
+ `templebars` allows you to precomile [Handlebars][handlebars] templates
4
+ in Rails and make them available globally on the client-side in a
5
+ `Templates` object.
4
6
 
5
- ```
6
- app/assets/javascripts/templates/todo_item.js.handlebars
7
- ```
7
+ ## Adding Templebars to Rails 3.1+
8
8
 
9
- You can then access it like any other JavaScript asset:
9
+ Add `templebars` to your `Gemfile` in the `assets` group:
10
10
 
11
11
  ```ruby
12
- javascript_include_tag( "templates/todo_item" )
12
+ group :assets do
13
+ ...
14
+ gem 'templebars'
15
+ end
13
16
  ```
14
17
 
15
- The above template would be stored as `Templates.todo_item`.
18
+ And run `bundle install` to install it.
16
19
 
17
- ## Handlebars
20
+ Then, you'll need to include the Handlebars runtime in your JavaScript
21
+ manifest (`app/assets/javascripts/application.js`):
22
+
23
+ ```javascript
24
+ //= require handlebars.runtime
25
+ ```
18
26
 
19
- This gem also provides Handlebars 1.0.beta.6 to the Rails assert pipeline via `handlebars`. You can include it in other JS files:
27
+ If you still need to compile Handlebars templates on the client side,
28
+ you'll want to require the full `handlebars`, instead:
20
29
 
21
- ```js
30
+ ```javascript
22
31
  //= require handlebars
23
32
  ```
24
33
 
25
- You can also include it via a regular ol' `javascript_include_tag` call:
34
+ ## Adding templates
26
35
 
27
- ```ruby
28
- javascript_include_tag( "handlebars" )
36
+ Place your templates in `app/assets/templates/` and require all of them
37
+ in your JavaScript manifest (`application.js`):
38
+
39
+ ```javascript
40
+ //= require_tree ../templates
29
41
  ```
30
42
 
31
- ## Installation
43
+ Your template file names must be suffixed with the ".handlebars" or
44
+ ".hbs" extensions in order for Templebars to handle them.
45
+
46
+ ## Using templates
47
+
48
+ Your templates will be available on the client side via their paths in a
49
+ global `Templates` object. For example, a template at
50
+ `app/assets/templates/user.handlebars` can be rendered with:
51
+
52
+ ```javascript
53
+ Templates['user'](context);
54
+ ```
55
+
56
+ And a template at `app/assets/templates/users/detail.handlebars` with:
57
+
58
+ ```javascript
59
+ Templates['users/detail'](context);
60
+ ```
32
61
 
33
- Add this to your Gemfile, preferably in the `:assets` gem group:
62
+ *This gem is maintained by [Stovepipe Studios][stovepipe].*
34
63
 
35
- gem "templebars"
64
+ [stovepipe]: http://www.stovepipestudios.com
65
+ [handlebars]: http://handlebarsjs.com/
36
66
 
37
- Done. Go forth, grasshopper.
@@ -11,21 +11,23 @@ module Templebars
11
11
  "application/javascript"
12
12
  end
13
13
 
14
- def initialize_engine; end
15
-
16
14
  def prepare; end
17
15
 
18
16
  def evaluate( scope, locals, &block )
19
- global = Templebars::Rails::GLOBAL
20
- setup_global = "('undefined' == typeof #{global}) && (#{global} = {})"
21
17
  name = scope.logical_path.sub( /^templates\//, "" )
22
- precompiled_js = precompile( data )
23
- template_declaration = "#{global}[\"#{name}\"] = Handlebars.template(#{precompiled_js});"
24
- "#{setup_global}\n#{template_declaration}\n"
18
+ register_template_js( name, precompile( data ) )
25
19
  end
26
20
 
27
21
  protected
28
22
 
23
+ def register_template_js( name, precompiled_js )
24
+ global = Templebars::Rails::GLOBAL
25
+ <<-JS
26
+ this.#{global} || (this.#{global} = {});
27
+ this.#{global}["#{name}"] = Handlebars.template(#{precompiled_js});
28
+ JS
29
+ end
30
+
29
31
  def precompile( template )
30
32
  runtime.call( "Handlebars.precompile", template, { data: {} } )
31
33
  end
@@ -35,13 +37,13 @@ module Templebars
35
37
  end
36
38
 
37
39
  def handlebars_js
38
- path = File.join( File.dirname(__FILE__), "..", "..", "..", "vendor", "assets", "javascripts", "handlebars.js" )
40
+ path = File.expand_path( "../../../vendor/assets/javascripts/handlebars.js" )
39
41
  File.read( path )
40
42
  end
41
43
  end
42
44
 
43
45
  Sprockets.register_engine '.handlebars', HandlebarsTemplate
44
46
  Sprockets.register_engine '.hbs', HandlebarsTemplate
45
- Sprockets.register_engine '.hb', HandlebarsTemplate
46
47
  end
47
48
  end
49
+
@@ -1,6 +1,6 @@
1
1
  module Templebars
2
2
  module Rails
3
- VERSION = "0.0.6"
4
- HANDLEBARSJS_VERSION = "1.0.beta.5"
3
+ VERSION = "0.0.7"
4
+ HANDLEBARSJS_VERSION = "1.0.beta.6"
5
5
  end
6
6
  end
data/templebars.gemspec CHANGED
@@ -14,8 +14,10 @@ Gem::Specification.new do |s|
14
14
  s.required_rubygems_version = ">= 1.3.6"
15
15
  s.rubyforge_project = "templebars"
16
16
 
17
- s.add_dependency "railties", ">= 3.2.0"
17
+ s.add_dependency "sprockets", ">= 2.0.3"
18
18
  s.add_dependency "execjs"
19
+ s.add_dependency "tilt"
20
+ s.add_development_dependency "rake"
19
21
 
20
22
  s.files = `git ls-files`.split("\n")
21
23
  s.require_path = 'lib'
@@ -0,0 +1,223 @@
1
+ // lib/handlebars/base.js
2
+ var Handlebars = {};
3
+
4
+ Handlebars.VERSION = "1.0.beta.6";
5
+
6
+ Handlebars.helpers = {};
7
+ Handlebars.partials = {};
8
+
9
+ Handlebars.registerHelper = function(name, fn, inverse) {
10
+ if(inverse) { fn.not = inverse; }
11
+ this.helpers[name] = fn;
12
+ };
13
+
14
+ Handlebars.registerPartial = function(name, str) {
15
+ this.partials[name] = str;
16
+ };
17
+
18
+ Handlebars.registerHelper('helperMissing', function(arg) {
19
+ if(arguments.length === 2) {
20
+ return undefined;
21
+ } else {
22
+ throw new Error("Could not find property '" + arg + "'");
23
+ }
24
+ });
25
+
26
+ var toString = Object.prototype.toString, functionType = "[object Function]";
27
+
28
+ Handlebars.registerHelper('blockHelperMissing', function(context, options) {
29
+ var inverse = options.inverse || function() {}, fn = options.fn;
30
+
31
+
32
+ var ret = "";
33
+ var type = toString.call(context);
34
+
35
+ if(type === functionType) { context = context.call(this); }
36
+
37
+ if(context === true) {
38
+ return fn(this);
39
+ } else if(context === false || context == null) {
40
+ return inverse(this);
41
+ } else if(type === "[object Array]") {
42
+ if(context.length > 0) {
43
+ for(var i=0, j=context.length; i<j; i++) {
44
+ ret = ret + fn(context[i]);
45
+ }
46
+ } else {
47
+ ret = inverse(this);
48
+ }
49
+ return ret;
50
+ } else {
51
+ return fn(context);
52
+ }
53
+ });
54
+
55
+ Handlebars.registerHelper('each', function(context, options) {
56
+ var fn = options.fn, inverse = options.inverse;
57
+ var ret = "";
58
+
59
+ if(context && context.length > 0) {
60
+ for(var i=0, j=context.length; i<j; i++) {
61
+ ret = ret + fn(context[i]);
62
+ }
63
+ } else {
64
+ ret = inverse(this);
65
+ }
66
+ return ret;
67
+ });
68
+
69
+ Handlebars.registerHelper('if', function(context, options) {
70
+ var type = toString.call(context);
71
+ if(type === functionType) { context = context.call(this); }
72
+
73
+ if(!context || Handlebars.Utils.isEmpty(context)) {
74
+ return options.inverse(this);
75
+ } else {
76
+ return options.fn(this);
77
+ }
78
+ });
79
+
80
+ Handlebars.registerHelper('unless', function(context, options) {
81
+ var fn = options.fn, inverse = options.inverse;
82
+ options.fn = inverse;
83
+ options.inverse = fn;
84
+
85
+ return Handlebars.helpers['if'].call(this, context, options);
86
+ });
87
+
88
+ Handlebars.registerHelper('with', function(context, options) {
89
+ return options.fn(context);
90
+ });
91
+
92
+ Handlebars.registerHelper('log', function(context) {
93
+ Handlebars.log(context);
94
+ });
95
+ ;
96
+ // lib/handlebars/utils.js
97
+ Handlebars.Exception = function(message) {
98
+ var tmp = Error.prototype.constructor.apply(this, arguments);
99
+
100
+ for (var p in tmp) {
101
+ if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; }
102
+ }
103
+
104
+ this.message = tmp.message;
105
+ };
106
+ Handlebars.Exception.prototype = new Error;
107
+
108
+ // Build out our basic SafeString type
109
+ Handlebars.SafeString = function(string) {
110
+ this.string = string;
111
+ };
112
+ Handlebars.SafeString.prototype.toString = function() {
113
+ return this.string.toString();
114
+ };
115
+
116
+ (function() {
117
+ var escape = {
118
+ "<": "&lt;",
119
+ ">": "&gt;",
120
+ '"': "&quot;",
121
+ "'": "&#x27;",
122
+ "`": "&#x60;"
123
+ };
124
+
125
+ var badChars = /&(?!\w+;)|[<>"'`]/g;
126
+ var possible = /[&<>"'`]/;
127
+
128
+ var escapeChar = function(chr) {
129
+ return escape[chr] || "&amp;";
130
+ };
131
+
132
+ Handlebars.Utils = {
133
+ escapeExpression: function(string) {
134
+ // don't escape SafeStrings, since they're already safe
135
+ if (string instanceof Handlebars.SafeString) {
136
+ return string.toString();
137
+ } else if (string == null || string === false) {
138
+ return "";
139
+ }
140
+
141
+ if(!possible.test(string)) { return string; }
142
+ return string.replace(badChars, escapeChar);
143
+ },
144
+
145
+ isEmpty: function(value) {
146
+ if (typeof value === "undefined") {
147
+ return true;
148
+ } else if (value === null) {
149
+ return true;
150
+ } else if (value === false) {
151
+ return true;
152
+ } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
153
+ return true;
154
+ } else {
155
+ return false;
156
+ }
157
+ }
158
+ };
159
+ })();;
160
+ // lib/handlebars/runtime.js
161
+ Handlebars.VM = {
162
+ template: function(templateSpec) {
163
+ // Just add water
164
+ var container = {
165
+ escapeExpression: Handlebars.Utils.escapeExpression,
166
+ invokePartial: Handlebars.VM.invokePartial,
167
+ programs: [],
168
+ program: function(i, fn, data) {
169
+ var programWrapper = this.programs[i];
170
+ if(data) {
171
+ return Handlebars.VM.program(fn, data);
172
+ } else if(programWrapper) {
173
+ return programWrapper;
174
+ } else {
175
+ programWrapper = this.programs[i] = Handlebars.VM.program(fn);
176
+ return programWrapper;
177
+ }
178
+ },
179
+ programWithDepth: Handlebars.VM.programWithDepth,
180
+ noop: Handlebars.VM.noop
181
+ };
182
+
183
+ return function(context, options) {
184
+ options = options || {};
185
+ return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
186
+ };
187
+ },
188
+
189
+ programWithDepth: function(fn, data, $depth) {
190
+ var args = Array.prototype.slice.call(arguments, 2);
191
+
192
+ return function(context, options) {
193
+ options = options || {};
194
+
195
+ return fn.apply(this, [context, options.data || data].concat(args));
196
+ };
197
+ },
198
+ program: function(fn, data) {
199
+ return function(context, options) {
200
+ options = options || {};
201
+
202
+ return fn(context, options.data || data);
203
+ };
204
+ },
205
+ noop: function() { return ""; },
206
+ invokePartial: function(partial, name, context, helpers, partials, data) {
207
+ options = { helpers: helpers, partials: partials, data: data };
208
+
209
+ if(partial === undefined) {
210
+ throw new Handlebars.Exception("The partial " + name + " could not be found");
211
+ } else if(partial instanceof Function) {
212
+ return partial(context, options);
213
+ } else if (!Handlebars.compile) {
214
+ throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
215
+ } else {
216
+ partials[name] = Handlebars.compile(partial);
217
+ return partials[name](context, options);
218
+ }
219
+ }
220
+ };
221
+
222
+ Handlebars.template = Handlebars.VM.template;
223
+ ;
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: templebars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,22 +9,22 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-05 00:00:00.000000000 Z
12
+ date: 2012-05-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: railties
16
- requirement: &70233412557020 !ruby/object:Gem::Requirement
15
+ name: sprockets
16
+ requirement: &70293659066120 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
- version: 3.2.0
21
+ version: 2.0.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70233412557020
24
+ version_requirements: *70293659066120
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: execjs
27
- requirement: &70233412556460 !ruby/object:Gem::Requirement
27
+ requirement: &70293659065680 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,29 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70233412556460
35
+ version_requirements: *70293659065680
36
+ - !ruby/object:Gem::Dependency
37
+ name: tilt
38
+ requirement: &70293659083960 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70293659083960
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &70293659083520 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70293659083520
36
58
  description: This gem provides Handlebars magic for your Rails 3 application.
37
59
  email:
38
60
  - tyson@stovepipestudios.com
@@ -53,6 +75,7 @@ files:
53
75
  - lib/templebars/rails/version.rb
54
76
  - templebars.gemspec
55
77
  - vendor/assets/javascripts/handlebars.js
78
+ - vendor/assets/javascripts/handlebars.runtime.js
56
79
  homepage: http://rubygems.org/gems/templebars
57
80
  licenses: []
58
81
  post_install_message:
@@ -78,3 +101,4 @@ signing_key:
78
101
  specification_version: 3
79
102
  summary: Use precompiled Handlebars templates with Rails 3
80
103
  test_files: []
104
+ has_rdoc: