handlebars 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.mdown CHANGED
@@ -6,18 +6,18 @@ This uses [therubyracer][1] to bind to the _actual_ JavaScript implementation of
6
6
 
7
7
  ## Usage
8
8
 
9
- Simple stuff
9
+ ### Simple stuff
10
10
 
11
11
  require 'handlebars'
12
12
  handlebars = Handlebars::Context.new
13
13
  template = handlebars.compile("{{say}}{{what}}")
14
14
  template.call(:say => "Hey", :what => "Yuh!") #=> "Hey Yuh!"
15
15
 
16
- With functions as properties
16
+ ### functions as properties
17
17
 
18
18
  template.call(:say => "Hey", :what => lambda {|this| ("yo" * 2) + "!"}) #=> "Hey yoyo!"
19
19
 
20
- Register block helpers:
20
+ ### Block Helpers:
21
21
 
22
22
  handlebars.register_helper(:twice) do |block|
23
23
  "#{block.call}#{block.call}"
@@ -25,6 +25,41 @@ Register block helpers:
25
25
  template = handlebars.compile({{#twice}}Hurray!{{/twice}})
26
26
  template.call #=> Hurray!Hurray!
27
27
 
28
+ ### Safe Strings
29
+
30
+ By default, handlebars will escape strings that are returned by your block helpers. To
31
+ mark a string as safe:
32
+
33
+ template = handlebars.compile("{{safe}}")
34
+ template.call(:safe => proc {Handlebars::SafeString.new("<pre>Totally Safe!<pre>")})
35
+
36
+ ### Partials
37
+
38
+ You can directly register partials
39
+
40
+ handlebars.register_partial("whoami", " I am {{who}}")
41
+ handlebars.compile("{{>whoami}}").call(:who => 'Legend') #=> I am Legend
42
+
43
+ Partials can also be dynamically looked up by defining a partial_missing behavior:
44
+
45
+ handlebars.partial_missing do |name|
46
+ "unable to find >#{name}"
47
+ end
48
+ handlebars.compile("{{>missing}}").call #=> unable to find >missing
49
+
50
+ Missing partials can also be returned as a function:
51
+
52
+ count = 0
53
+ handlebars.partial_missing do |name|
54
+ lambda do |this, context, options|
55
+ count += 1
56
+ "#{count} miss(es) when trying to look up a partial"
57
+ end
58
+ end
59
+ t = handlebars.compile("{{>missing}}")
60
+ t.call #=> 1 miss(es) when trying to look up a partial
61
+ t.call #=> 2 miss(es) when tyring to look up a partial
62
+
28
63
  ## Hack
29
64
 
30
65
  git clone git@github.com:cowboyd/handlebars.rb.git #git it
@@ -33,13 +68,6 @@ Register block helpers:
33
68
  rspec spec/ #test it
34
69
 
35
70
 
36
- ## Rubygems
37
-
38
- Handlebars is available via the [hbs][3] gem on rubygems.org. There is also a
39
- [handlebars][4] gem, which is a pure-ruby implementation that appears to no longer be maintained.
40
-
41
71
  [1]: http://github.com/cowboyd/therubyracer "The Ruby Racer"
42
72
  [2]: http://github.com/wycats/handlebars.js "Handlebars JavaScript templating library"
43
- [3]: http://rubygems.org/gems/hbs "handelbars gem in JavaScript"
44
- [4]: http://rubygems.org/gems/handlebars "pure ruby handlebars gem"
45
73
 
data/lib/handlebars.rb CHANGED
@@ -1,6 +1,7 @@
1
1
 
2
2
  module Handlebars
3
- autoload :Context, 'handlebars/context'
4
- autoload :Template, 'handlebars/template'
5
- autoload :SafeString, 'handlebars/safe_string'
3
+ autoload :Context, 'handlebars/context'
4
+ autoload :Template, 'handlebars/template'
5
+ autoload :Partials, 'handlebars/partials'
6
+ autoload :SafeString, 'handlebars/safe_string'
6
7
  end
@@ -20,6 +20,7 @@ module Handlebars
20
20
  @js.require("handlebars/compiler/#{compiler_module}")
21
21
  end
22
22
  @js.require('handlebars/runtime')
23
+ @partials = handlebars.partials = Handlebars::Partials.new
23
24
  end
24
25
 
25
26
  def compile(*args)
@@ -34,6 +35,10 @@ module Handlebars
34
35
  handlebars.registerPartial(name, content)
35
36
  end
36
37
 
38
+ def partial_missing(&fn)
39
+ @partials.partial_missing = fn
40
+ end
41
+
37
42
  def handlebars
38
43
  @js.require('handlebars/base')
39
44
  end
@@ -0,0 +1,23 @@
1
+ module Handlebars
2
+ class Partials
3
+ attr_accessor :partial_missing
4
+
5
+ def initialize
6
+ @partials = {}
7
+ end
8
+
9
+ def []=(name, value)
10
+ @partials[name.to_s] = value
11
+ end
12
+
13
+ def [](name)
14
+ if @partials.has_key?(name.to_s)
15
+ return @partials[name.to_s]
16
+ elsif @partial_missing
17
+ return @partial_missing[name]
18
+ else
19
+ yield
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Handlebars
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -3,7 +3,7 @@ require 'handlebars'
3
3
  describe(Handlebars::Context) do
4
4
 
5
5
  describe "a simple template" do
6
- let(:t) {subject.compile("Hello {{name}}")}
6
+ let(:t) {compile("Hello {{name}}")}
7
7
  it "allows simple subsitution" do
8
8
  t.call(:name => 'World').should eql "Hello World"
9
9
  end
@@ -28,22 +28,40 @@ describe(Handlebars::Context) do
28
28
  end
29
29
 
30
30
  it "correctly passes context and implementation" do
31
- t = subject.compile("it's so {{#alsowith weather}}*{{summary}}*{{/alsowith}}!")
31
+ t = compile("it's so {{#alsowith weather}}*{{summary}}*{{/alsowith}}!")
32
32
  t.call(:weather => {:summary => "sunny"}).should eql "it's so *sunny*!"
33
33
  end
34
34
 
35
35
  it "doesn't nee a context or arguments to the call" do
36
- t = subject.compile("{{#twice}}Hurray!{{/twice}}")
36
+ t = compile("{{#twice}}Hurray!{{/twice}}")
37
37
  t.call.should eql "Hurray!Hurray!"
38
38
  end
39
39
  end
40
40
 
41
41
  describe "registering Partials" do
42
42
  before do
43
- subject.register_partial('legend', 'I am {{legend}}')
43
+ subject.register_partial('legend', 'I am {{who}}')
44
44
  end
45
45
  it "renders partials" do
46
- t = subject.compile("{{> legend}}").call(:legend => 'Legend!').should eql "I am Legend!"
46
+ compile("{{> legend}}").call(:who => 'Legend!').should eql "I am Legend!"
47
+ end
48
+ end
49
+
50
+ describe "dynamically loading partial" do
51
+ it "can be done with a string" do
52
+ subject.partial_missing do |name|
53
+ "unable to find >#{name}"
54
+ end
55
+ compile("I am {{>missing}}").call().should eql "I am unable to find >missing"
56
+ end
57
+
58
+ it "can be done with a function" do
59
+ subject.partial_missing do |name|
60
+ lambda do |this, context, options|
61
+ "unable to find >#{name}"
62
+ end
63
+ end
64
+ compile("I am {{>missing}}").call().should eql "I am unable to find >missing"
47
65
  end
48
66
  end
49
67
 
@@ -53,4 +71,8 @@ describe(Handlebars::Context) do
53
71
  t.call(:safe => lambda {|this, *args| Handlebars::SafeString.new("<pre>totally safe</pre>")}).should eql "<pre>totally safe</pre>"
54
72
  end
55
73
  end
74
+
75
+ def compile(*args)
76
+ subject.compile(*args)
77
+ end
56
78
  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.3.0
4
+ version: 0.3.1
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-04-06 00:00:00.000000000 Z
12
+ date: 2012-04-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: therubyracer
16
- requirement: &2152688600 !ruby/object:Gem::Requirement
16
+ requirement: &2153055500 !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: *2152688600
24
+ version_requirements: *2153055500
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: commonjs
27
- requirement: &2152687860 !ruby/object:Gem::Requirement
27
+ requirement: &2153054020 !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: *2152687860
35
+ version_requirements: *2153054020
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &2152686640 !ruby/object:Gem::Requirement
38
+ requirement: &2153051440 !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: *2152686640
46
+ version_requirements: *2153051440
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,7 @@ files:
62
62
  - handlebars.gemspec
63
63
  - lib/handlebars.rb
64
64
  - lib/handlebars/context.rb
65
+ - lib/handlebars/partials.rb
65
66
  - lib/handlebars/safe_string.rb
66
67
  - lib/handlebars/template.rb
67
68
  - lib/handlebars/version.rb