tilt-handlebars 1.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -42,10 +42,57 @@ Output:
42
42
 
43
43
  Hello, Joe. I'm happy to meet you.
44
44
 
45
+ ### Partials
46
+
47
+ Partials are a file that can be loaded into another. For example, you may define a web page with
48
+ a master layout (`layout.hbs`), which includes a header (`header.hbs`) and footer (`footer.hbs`).
49
+ In this case, `header.hbs` and `footer.hbs` would be partials; `layout.hbs` includes these partials.
50
+
51
+ `layout.hbs`:
52
+
53
+ ```html
54
+ <html>
55
+ <head>...</head>
56
+ <body>
57
+ {{> header }}
58
+
59
+ {{ content }}
60
+
61
+ {{> footer }}
62
+ </body>
63
+ </html>
64
+ ```
65
+
66
+ Notice that you do not include the `.hbs` file extension in the partial name. Tilt Handlebars
67
+ will look for the partial relative to the enclosing file (`layout.hbs` in this example) with
68
+ either a `.hbs` or `.handlebars` extension.
69
+
70
+ #### Sinatra
71
+
72
+ Handlebars can be used with Sintra:
73
+
74
+ ```ruby
75
+ require 'sinatra/handlebars'
76
+
77
+ class MyApp < Sinatra::Base
78
+ helpers Sinatra::Handlebars
79
+
80
+ get "/hello" do
81
+ handlebars :hello, locals: {name: 'Joe'}
82
+ end
83
+ end
84
+ ```
85
+
86
+ This will use the template file named `views/hello.hbs`.
87
+
88
+ Partials can also be used with Sinatra. As described previously, partials will be loaded
89
+ relative to the enclosing template (e.g., in the `views` directory).
90
+
45
91
  ## Contributing
46
92
 
47
93
  1. Fork it
48
94
  2. Create your feature branch (`git checkout -b my-new-feature`)
49
- 3. Commit your changes (`git commit -am 'Add some feature'`)
50
- 4. Push to the branch (`git push origin my-new-feature`)
51
- 5. Create new Pull Request
95
+ 3. Add tests, preferrably using Minitest::Spec for consistency.
96
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
97
+ 5. Push to the branch (`git push origin my-new-feature`)
98
+ 6. Create new Pull Request
@@ -0,0 +1,13 @@
1
+ Version 1.1.0
2
+ -------------
3
+ 2013 August 15
4
+
5
+ * Sinatra support
6
+ * Automatic loading of partials
7
+
8
+
9
+ Version 1.0.0
10
+ -------------
11
+ 2013 July 5
12
+
13
+ * Initial release
@@ -0,0 +1,9 @@
1
+ module Sinatra
2
+ module Handlebars
3
+ def handlebars(*args)
4
+ render(:handlebars, *args)
5
+ end
6
+ end
7
+
8
+ helpers Handlebars
9
+ end
@@ -13,18 +13,17 @@ module Tilt
13
13
  #
14
14
  class HandlebarsTemplate < Template
15
15
  def initialize_engine
16
- @context = ::Handlebars::Context.new
16
+ return if defined? ::Handlebars
17
+ require_template_library 'handlebars'
17
18
  end
18
19
 
19
- def self.engine_initialized?
20
- true
21
- end
22
-
23
20
  def prepare
24
21
  @context = ::Handlebars::Context.new
25
- @template = @context.compile(data)
22
+ @context.partial_missing { |partial_name| load_partial partial_name }
23
+ @template = @context.compile(data)
26
24
  end
27
25
 
26
+
28
27
  def evaluate(scope, locals = {}, &block)
29
28
  # Based on LiquidTemplate
30
29
  locals = locals.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
@@ -59,6 +58,25 @@ module Tilt
59
58
  def allows_script?
60
59
  false
61
60
  end
61
+
62
+ def load_partial(partial_name)
63
+ if Pathname.new(partial_name).absolute?
64
+ dir = ""
65
+ elsif file
66
+ dir = File.dirname file
67
+ end
68
+
69
+ partial_file = File.expand_path("#{partial_name}.hbs", dir)
70
+ partial_file = File.expand_path("#{partial_name}.handlebars", dir) unless File.file? partial_file
71
+ if File.file? partial_file
72
+ return IO.read(partial_file)
73
+ end
74
+
75
+ raise "The partial '#{partial_name}' could not be found. No such file #{partial_file}"
76
+ end
77
+
78
+ private :load_partial
79
+
62
80
  end
63
81
 
64
82
  register HandlebarsTemplate, 'handlebars', 'hbs'
@@ -0,0 +1,5 @@
1
+ module Tilt
2
+ module Handlebars
3
+ VERSION = "1.1.0"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ Has anyone seen my partial? {{> no_partial }}
@@ -0,0 +1 @@
1
+ It Came From the Partial Side by {{ author }}
@@ -0,0 +1 @@
1
+ It Came From the Partial Side by {{ author }}
@@ -0,0 +1 @@
1
+ My all time favorite book is {{> partial}}.
@@ -0,0 +1 @@
1
+ My all time favorite book is {{> partial2}}.
@@ -0,0 +1 @@
1
+ {{> partial}} may be a good book, but I'm waiting for the movie directed by {{> director}}.
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ require 'rack/test'
4
+ require 'sinatra/base'
5
+ require 'sinatra/handlebars'
6
+
7
+ class HandlebarsApp < Sinatra::Base
8
+ set :root, File.dirname(__FILE__) + '/fixtures'
9
+ helpers Sinatra::Handlebars
10
+
11
+ get "/hello" do
12
+ handlebars :hello, locals: {name: 'Joe', emotion: 'happy'}
13
+ end
14
+
15
+ get "/partials" do
16
+ handlebars :partial_test, locals: {author: "Stephanie Queen"}
17
+ end
18
+ end
19
+
20
+
21
+ describe "Using handlebars in Sinatra" do
22
+ include Rack::Test::Methods
23
+
24
+ def app
25
+ HandlebarsApp
26
+ end
27
+
28
+ it "renders simple template" do
29
+ response = get "/hello"
30
+ response.body.strip.must_equal "Hello, Joe. I'm happy to meet you."
31
+ end
32
+
33
+ it "renders template with partials" do
34
+ response = get "/partials"
35
+ response.body.strip.must_equal "My all time favorite book is It Came From the Partial Side by Stephanie Queen."
36
+ end
37
+
38
+ end
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ require 'minitest/autorun'
5
+ require 'minitest/spec'
@@ -1,8 +1,4 @@
1
- require 'bundler'
2
- Bundler.setup
3
-
4
- require 'minitest/autorun'
5
- require 'minitest/spec'
1
+ require 'test_helper'
6
2
 
7
3
  require 'tilt'
8
4
  require 'tilt/handlebars'
@@ -13,7 +9,7 @@ end
13
9
 
14
10
  describe Tilt::HandlebarsTemplate do
15
11
  it 'renders from file' do
16
- template = Tilt.new('test/hello.hbs')
12
+ template = Tilt.new('test/fixtures/views/hello.hbs')
17
13
  template.render(nil, name: 'Joe', emotion: 'happy').must_equal "Hello, Joe. I'm happy to meet you.\n"
18
14
  end
19
15
 
@@ -116,7 +112,26 @@ describe Tilt::HandlebarsTemplate do
116
112
  end
117
113
 
118
114
  describe "helpers" do
119
- it "applies helper to static text" do
115
+
116
+ it "applies simple helper to value" do
117
+ template = make_template '{{upper greeting}}, world'
118
+ template.register_helper(:upper) do |this, *args|
119
+ "#{args[0].upcase}"
120
+ end
121
+
122
+ template.render(nil, :greeting => 'hello').must_equal "HELLO, world"
123
+ end
124
+
125
+ it "applies simple helper to quoted value" do
126
+ template = make_template '{{upper "greetings and salutations"}}, world'
127
+ template.register_helper(:upper) do |this, *args|
128
+ "#{args[0].upcase}"
129
+ end
130
+
131
+ template.render.must_equal "GREETINGS AND SALUTATIONS, world"
132
+ end
133
+
134
+ it "applies block helper to static text" do
120
135
  template = make_template '{{#upper}}Hello, World.{{/upper}}'
121
136
  template.register_helper(:upper) do |this, block|
122
137
  block.fn(this).upcase
@@ -125,7 +140,7 @@ describe Tilt::HandlebarsTemplate do
125
140
  template.render.must_equal "HELLO, WORLD."
126
141
  end
127
142
 
128
- it "applies helper to nested values" do
143
+ it "applies block helper to nested values" do
129
144
  template = make_template '{{#upper}}Hey {{name}}{{/upper}}!'
130
145
  template.register_helper(:upper) do |this, block|
131
146
  block.fn(this).upcase
@@ -203,13 +218,52 @@ describe Tilt::HandlebarsTemplate do
203
218
  end
204
219
 
205
220
  describe "partials" do
206
- it "displays content of partial" do
221
+ it "looks for partial relative to the template file" do
222
+ template = Tilt.new('test/fixtures/views/partial_test.hbs')
223
+ template.render(nil, :author => "Stephanie Queen").must_equal "My all time favorite book is It Came From the Partial Side by Stephanie Queen."
224
+ end
225
+
226
+ it "can load partial from absolute path" do
227
+ dir = Dir.pwd
228
+ template = make_template "Have you read {{> #{dir}/test/fixtures/views/partial}}?"
229
+ template.render(nil, :author => "Stephanie Queen").must_equal "Have you read It Came From the Partial Side by Stephanie Queen?"
230
+ end
231
+
232
+ it "also recognizes .handlebars extension" do
233
+ template = Tilt.new('test/fixtures/views/partial_test2.handlebars')
234
+ template.render(nil, :author => "Stephanie Queen").must_equal "My all time favorite book is It Came From the Partial Side by Stephanie Queen."
235
+ end
236
+
237
+ it "can load relative partial along with registered partial" do
238
+ template = Tilt.new('test/fixtures/views/two_partials.hbs')
239
+ template.register_partial :director, "Gary Rockhammer"
240
+ template.render(nil, :author => "Stephanie Queen").must_equal "It Came From the Partial Side by Stephanie Queen may be a good book, but I'm waiting for the movie directed by Gary Rockhammer."
241
+ end
242
+
243
+ it "gives precedence to registered partial over relative file" do
244
+ template = Tilt.new('test/fixtures/views/partial_test.hbs')
245
+ template.register_partial :partial, "Revenge of the Partial"
246
+ template.render.must_equal "My all time favorite book is Revenge of the Partial."
247
+ end
248
+
249
+ it "raises error if partial cannot be found" do
250
+ template = Tilt.new('test/fixtures/views/missing_partial.hbs')
251
+ # template.render
252
+ proc { template.render }.must_raise V8::Error
253
+ end
254
+
255
+ it "cannot automatically load partial when template is created from string instead of file" do
256
+ template = make_template "I wish I could load a partial like this: {{> my_partial}}."
257
+ proc { template.render }.must_raise V8::Error
258
+ end
259
+
260
+ it "allows partial to be registered" do
207
261
  template = make_template "{{> greeting}}. Nice to meet you."
208
262
  template.register_partial :greeting, "Hey, {{name}}"
209
263
  template.render(nil, :name => "Joe").must_equal "Hey, Joe. Nice to meet you."
210
264
  end
211
265
 
212
- it "calls registeded method if partial is missing" do
266
+ it "calls registered method if partial is missing" do
213
267
  template = make_template "{{> where}} I've been looking for you."
214
268
  template.partial_missing do |partial_name|
215
269
  "Where have you been, {{name}}?" if partial_name == 'where'
@@ -2,9 +2,11 @@
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
+ require 'tilt/handlebars/version'
6
+
5
7
  Gem::Specification.new do |spec|
6
8
  spec.name = "tilt-handlebars"
7
- spec.version = "1.0"
9
+ spec.version = Tilt::Handlebars::VERSION
8
10
  spec.authors = ["Jim Cushing"]
9
11
  spec.email = ["jimothy@mac.com"]
10
12
  spec.description = "Use Handlebars.rb with Tilt"
@@ -17,9 +19,12 @@ Gem::Specification.new do |spec|
17
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
20
  spec.require_paths = ["lib"]
19
21
 
22
+ spec.add_dependency "tilt", "~> 1.4"
23
+ spec.add_dependency "handlebars", "~> 0.4"
24
+
20
25
  spec.add_development_dependency "bundler", "~> 1.3"
21
26
  spec.add_development_dependency "rake"
22
27
  spec.add_development_dependency "minitest", "~> 5.0"
23
- spec.add_dependency "tilt", "~> 1.4"
24
- spec.add_dependency "handlebars", "~> 0.4"
28
+ spec.add_development_dependency "sinatra"
29
+ spec.add_development_dependency "rack-test"
25
30
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tilt-handlebars
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.0'
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,40 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-05 00:00:00.000000000 Z
12
+ date: 2013-08-15 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: tilt
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.4'
30
+ - !ruby/object:Gem::Dependency
31
+ name: handlebars
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '0.4'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '0.4'
14
46
  - !ruby/object:Gem::Dependency
15
47
  name: bundler
16
48
  requirement: !ruby/object:Gem::Requirement
@@ -60,37 +92,37 @@ dependencies:
60
92
  - !ruby/object:Gem::Version
61
93
  version: '5.0'
62
94
  - !ruby/object:Gem::Dependency
63
- name: tilt
95
+ name: sinatra
64
96
  requirement: !ruby/object:Gem::Requirement
65
97
  none: false
66
98
  requirements:
67
- - - ~>
99
+ - - ! '>='
68
100
  - !ruby/object:Gem::Version
69
- version: '1.4'
70
- type: :runtime
101
+ version: '0'
102
+ type: :development
71
103
  prerelease: false
72
104
  version_requirements: !ruby/object:Gem::Requirement
73
105
  none: false
74
106
  requirements:
75
- - - ~>
107
+ - - ! '>='
76
108
  - !ruby/object:Gem::Version
77
- version: '1.4'
109
+ version: '0'
78
110
  - !ruby/object:Gem::Dependency
79
- name: handlebars
111
+ name: rack-test
80
112
  requirement: !ruby/object:Gem::Requirement
81
113
  none: false
82
114
  requirements:
83
- - - ~>
115
+ - - ! '>='
84
116
  - !ruby/object:Gem::Version
85
- version: '0.4'
86
- type: :runtime
117
+ version: '0'
118
+ type: :development
87
119
  prerelease: false
88
120
  version_requirements: !ruby/object:Gem::Requirement
89
121
  none: false
90
122
  requirements:
91
- - - ~>
123
+ - - ! '>='
92
124
  - !ruby/object:Gem::Version
93
- version: '0.4'
125
+ version: '0'
94
126
  description: Use Handlebars.rb with Tilt
95
127
  email:
96
128
  - jimothy@mac.com
@@ -102,9 +134,20 @@ files:
102
134
  - Gemfile
103
135
  - LICENSE.txt
104
136
  - README.md
137
+ - RELEASE_NOTES.md
105
138
  - Rakefile
139
+ - lib/sinatra/handlebars.rb
106
140
  - lib/tilt/handlebars.rb
107
- - test/hello.hbs
141
+ - lib/tilt/handlebars/version.rb
142
+ - test/fixtures/views/hello.hbs
143
+ - test/fixtures/views/missing_partial.hbs
144
+ - test/fixtures/views/partial.hbs
145
+ - test/fixtures/views/partial2.handlebars
146
+ - test/fixtures/views/partial_test.hbs
147
+ - test/fixtures/views/partial_test2.handlebars
148
+ - test/fixtures/views/two_partials.hbs
149
+ - test/sinatra_test.rb
150
+ - test/test_helper.rb
108
151
  - test/tilt_handlebarstemplate_test.rb
109
152
  - tilt-handlebars.gemspec
110
153
  homepage: https://github.com/jimothyGator/tilt-handlebars
@@ -120,24 +163,26 @@ required_ruby_version: !ruby/object:Gem::Requirement
120
163
  - - ! '>='
121
164
  - !ruby/object:Gem::Version
122
165
  version: '0'
123
- segments:
124
- - 0
125
- hash: -129076644092719377
126
166
  required_rubygems_version: !ruby/object:Gem::Requirement
127
167
  none: false
128
168
  requirements:
129
169
  - - ! '>='
130
170
  - !ruby/object:Gem::Version
131
171
  version: '0'
132
- segments:
133
- - 0
134
- hash: -129076644092719377
135
172
  requirements: []
136
173
  rubyforge_project:
137
- rubygems_version: 1.8.25
174
+ rubygems_version: 1.8.23
138
175
  signing_key:
139
176
  specification_version: 3
140
177
  summary: Use Handlebars.rb with Tilt
141
178
  test_files:
142
- - test/hello.hbs
179
+ - test/fixtures/views/hello.hbs
180
+ - test/fixtures/views/missing_partial.hbs
181
+ - test/fixtures/views/partial.hbs
182
+ - test/fixtures/views/partial2.handlebars
183
+ - test/fixtures/views/partial_test.hbs
184
+ - test/fixtures/views/partial_test2.handlebars
185
+ - test/fixtures/views/two_partials.hbs
186
+ - test/sinatra_test.rb
187
+ - test/test_helper.rb
143
188
  - test/tilt_handlebarstemplate_test.rb