handlebars 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/handlebars.rb CHANGED
@@ -1,4 +1,6 @@
1
1
 
2
2
  module Handlebars
3
- autoload :Context, 'handlebars/context'
3
+ autoload :Context, 'handlebars/context'
4
+ autoload :Template, 'handlebars/template'
5
+ autoload :SafeString, 'handlebars/safe_string'
4
6
  end
@@ -23,16 +23,23 @@ module Handlebars
23
23
  end
24
24
 
25
25
  def compile(*args)
26
- handlebars.compile(*args)
26
+ ::Handlebars::Template.new(self, handlebars.compile(*args))
27
27
  end
28
28
 
29
29
  def register_helper(name, &fn)
30
30
  handlebars.registerHelper(name, fn)
31
31
  end
32
32
 
33
+ def register_partial(name, content)
34
+ handlebars.registerPartial(name, content)
35
+ end
36
+
33
37
  def handlebars
34
38
  @js.require('handlebars/base')
35
39
  end
36
40
 
41
+ class << self
42
+ attr_accessor :current
43
+ end
37
44
  end
38
45
  end
@@ -0,0 +1,11 @@
1
+ module Handlebars
2
+ class SafeString
3
+ def self.new(string)
4
+ if context = Context.current
5
+ context.handlebars['SafeString'].new(string)
6
+ else
7
+ fail "Cannot instantiate Handlebars.SafeString outside a running template Evaluation"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,15 @@
1
+ module Handlebars
2
+ class Template
3
+ def initialize(context, fn)
4
+ @context, @fn = context, fn
5
+ end
6
+
7
+ def call(*args)
8
+ current = Handlebars::Context.current
9
+ Handlebars::Context.current = @context
10
+ @fn.call(*args)
11
+ ensure
12
+ Handlebars::Context.current = current
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Handlebars
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -37,4 +37,20 @@ describe(Handlebars::Context) do
37
37
  t.call.should eql "Hurray!Hurray!"
38
38
  end
39
39
  end
40
+
41
+ describe "registering Partials" do
42
+ before do
43
+ subject.register_partial('legend', 'I am {{legend}}')
44
+ end
45
+ it "renders partials" do
46
+ t = subject.compile("{{> legend}}").call(:legend => 'Legend!').should eql "I am Legend!"
47
+ end
48
+ end
49
+
50
+ describe "creating safe strings from ruby" do
51
+ let(:t) {subject.compile("{{safe}}")}
52
+ it "respects safe strings returned from ruby blocks" do
53
+ t.call(:safe => lambda {|this, *args| Handlebars::SafeString.new("<pre>totally safe</pre>")}).should eql "<pre>totally safe</pre>"
54
+ end
55
+ end
40
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handlebars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-28 00:00:00.000000000 Z
12
+ date: 2012-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: therubyracer
16
- requirement: &2152330120 !ruby/object:Gem::Requirement
16
+ requirement: &2152688600 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.10.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2152330120
24
+ version_requirements: *2152688600
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: commonjs
27
- requirement: &2152328500 !ruby/object:Gem::Requirement
27
+ requirement: &2152687860 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.2.3
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2152328500
35
+ version_requirements: *2152687860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &2152325880 !ruby/object:Gem::Requirement
38
+ requirement: &2152686640 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '2.0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2152325880
46
+ version_requirements: *2152686640
47
47
  description: Uses the actual JavaScript implementation of Handlebars, but supports
48
48
  using Ruby objects as template contexts and Ruby procs as view functions and named
49
49
  helpers
@@ -62,6 +62,8 @@ files:
62
62
  - handlebars.gemspec
63
63
  - lib/handlebars.rb
64
64
  - lib/handlebars/context.rb
65
+ - lib/handlebars/safe_string.rb
66
+ - lib/handlebars/template.rb
65
67
  - lib/handlebars/version.rb
66
68
  - spec/handlebars_spec.rb
67
69
  - spike.rb