handlebars 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +1 -2
  3. data/README.mdown +15 -7
  4. data/handlebars.gemspec +2 -5
  5. data/lib/handlebars.rb +5 -5
  6. data/lib/handlebars/context.rb +3 -12
  7. data/lib/handlebars/version.rb +1 -1
  8. data/spec/handlebars_spec.rb +1 -1
  9. metadata +47 -58
  10. data/.gitmodules +0 -3
  11. data/vendor/handlebars/.gitignore +0 -8
  12. data/vendor/handlebars/.jshintrc +0 -52
  13. data/vendor/handlebars/.npmignore +0 -10
  14. data/vendor/handlebars/.rspec +0 -1
  15. data/vendor/handlebars/Gemfile +0 -5
  16. data/vendor/handlebars/LICENSE +0 -19
  17. data/vendor/handlebars/README.markdown +0 -317
  18. data/vendor/handlebars/Rakefile +0 -109
  19. data/vendor/handlebars/bench/benchwarmer.js +0 -149
  20. data/vendor/handlebars/bench/handlebars.js +0 -172
  21. data/vendor/handlebars/bin/handlebars +0 -193
  22. data/vendor/handlebars/dist/handlebars.js +0 -2201
  23. data/vendor/handlebars/dist/handlebars.runtime.js +0 -321
  24. data/vendor/handlebars/lib/handlebars.js +0 -14
  25. data/vendor/handlebars/lib/handlebars/base.js +0 -154
  26. data/vendor/handlebars/lib/handlebars/compiler/ast.js +0 -133
  27. data/vendor/handlebars/lib/handlebars/compiler/base.js +0 -21
  28. data/vendor/handlebars/lib/handlebars/compiler/compiler.js +0 -1267
  29. data/vendor/handlebars/lib/handlebars/compiler/index.js +0 -7
  30. data/vendor/handlebars/lib/handlebars/compiler/printer.js +0 -131
  31. data/vendor/handlebars/lib/handlebars/compiler/visitor.js +0 -13
  32. data/vendor/handlebars/lib/handlebars/runtime.js +0 -88
  33. data/vendor/handlebars/lib/handlebars/utils.js +0 -67
  34. data/vendor/handlebars/package.json +0 -35
  35. data/vendor/handlebars/spec/acceptance_spec.rb +0 -101
  36. data/vendor/handlebars/spec/parser_spec.rb +0 -433
  37. data/vendor/handlebars/spec/qunit_spec.js +0 -1370
  38. data/vendor/handlebars/spec/spec_helper.rb +0 -157
  39. data/vendor/handlebars/spec/tokenizer_spec.rb +0 -301
  40. data/vendor/handlebars/src/handlebars.l +0 -53
  41. data/vendor/handlebars/src/handlebars.yy +0 -109
  42. data/vendor/handlebars/src/parser-prefix.js +0 -1
  43. data/vendor/handlebars/src/parser-suffix.js +0 -4
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a4927efb0fc9b8d1a05f154d196dc5d8f20109d7
4
+ data.tar.gz: 3f3c1bf18f9ed01fbfd0fa9c0ec4aafa6ac2c4d5
5
+ SHA512:
6
+ metadata.gz: 13ce9a7b01337645f8ed31e3febc89bc2c3843c6aad0259aef37018b7755d9379eb273e72168b6ce4b292c1bd8d889b5de03ed59bba454387840e1e159a9cbc7
7
+ data.tar.gz: 6b05eb3cedcdf0d318e2640d09f79f00c504e9d8f4decd10768323ae915210bc1c9cf77c59ce1ca32de41b86961fd0186e2e01a67382c69474fbeb0ffb09ae96
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
- source "http://rubygems.org"
2
-
1
+ source "https://rubygems.org"
3
2
  # Specify your gem's dependencies in handlebars.gemspec
4
3
  gemspec
@@ -10,20 +10,28 @@ This uses [therubyracer][1] to bind to the _actual_ JavaScript implementation of
10
10
 
11
11
  require 'handlebars'
12
12
  handlebars = Handlebars::Context.new
13
- template = handlebars.compile("{{say}}{{what}}")
13
+ template = handlebars.compile("{{say}} {{what}}")
14
14
  template.call(:say => "Hey", :what => "Yuh!") #=> "Hey Yuh!"
15
15
 
16
16
  ### functions as properties
17
17
 
18
- template.call(:say => "Hey", :what => lambda {|this| ("yo" * 2) + "!"}) #=> "Hey yoyo!"
18
+ template.call(:say => "Hey ", :what => lambda {|this| ("yo" * 2) + "!"}) #=> "Hey yoyo!"
19
19
 
20
20
  ### Block Helpers:
21
21
 
22
- handlebars.register_helper(:twice) do |block|
23
- "#{block.call}#{block.call}"
22
+ Just like JavaScript, you can write block helpers with an `{{else}}` section. To print
23
+ out a section twice if a condition is met:
24
+
25
+ handlebars.register_helper(:twice) do |context, condition, block|
26
+ if condition
27
+ "#{block.fn(context)}#{block.fn(context)}"
28
+ else
29
+ block.inverse(context)
30
+ end
24
31
  end
25
- template = handlebars.compile({{#twice}}Hurray!{{/twice}})
26
- template.call #=> Hurray!Hurray!
32
+ template = handlebars.compile("{{#twice foo}}Hurray!{{else}}Boo!{{/twice}}")
33
+ template.call(foo: true) #=> Hurray!Hurray!
34
+ template.call(foo: false) #=> Boo!
27
35
 
28
36
  ### Safe Strings
29
37
 
@@ -37,7 +45,7 @@ mark a string as safe:
37
45
 
38
46
  You can directly register partials
39
47
 
40
- handlebars.register_partial("whoami", " I am {{who}}")
48
+ handlebars.register_partial("whoami", "I am {{who}}")
41
49
  handlebars.compile("{{>whoami}}").call(:who => 'Legend') #=> I am Legend
42
50
 
43
51
  Partials can also be dynamically looked up by defining a partial_missing behavior:
@@ -18,13 +18,10 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
- Dir.chdir("vendor/handlebars") do
22
- s.files += `git ls-files`.split("\n").map {|f| "vendor/handlebars/#{f}"}
23
- s.files += ['vendor/handlebars/lib/handlebars/compiler/parser.js']
24
- end
25
21
 
26
- s.add_dependency "therubyracer", "~> 0.11.1"
22
+ s.add_dependency "therubyracer", "~> 0.12.0"
27
23
  s.add_dependency "commonjs", "~> 0.2.3"
24
+ s.add_dependency "handlebars-source", "~> 1.0.12"
28
25
  s.add_development_dependency "rake"
29
26
  s.add_development_dependency "rspec", "~> 2.0"
30
27
  end
@@ -1,7 +1,7 @@
1
-
2
1
  module Handlebars
3
- autoload :Context, 'handlebars/context'
4
- autoload :Template, 'handlebars/template'
5
- autoload :Partials, 'handlebars/partials'
6
- autoload :SafeString, 'handlebars/safe_string'
7
2
  end
3
+
4
+ require 'handlebars/context'
5
+ require 'handlebars/template'
6
+ require 'handlebars/partials'
7
+ require 'handlebars/safe_string'
@@ -1,3 +1,4 @@
1
+ require 'handlebars/source'
1
2
  require 'commonjs'
2
3
  require 'v8'
3
4
 
@@ -5,19 +6,9 @@ module Handlebars
5
6
  class Context
6
7
  def initialize
7
8
  @js = CommonJS::Environment.new V8::Context.new, :path => [
8
- File.expand_path('../../../vendor/bootstrap', __FILE__),
9
- File.expand_path('../../../vendor/handlebars/lib', __FILE__)
9
+ File.dirname(Handlebars::Source.bundled_path)
10
10
  ]
11
11
 
12
- # This is a slightly modified version of handlebars.js found in the main
13
- # distribution. The Ruby commonjs environment does not support full directory
14
- # requires, so we expand them by hand. Eventually this may be fixed upstream
15
- # but right now I'm not sure if this is a node-specific extension.
16
-
17
- @js.require('handlebars/base')
18
- @js.require('handlebars/utils')
19
- @js.require('handlebars/compiler/index')
20
- @js.require('handlebars/runtime')
21
12
  @partials = handlebars.partials = Handlebars::Partials.new
22
13
  end
23
14
 
@@ -42,7 +33,7 @@ module Handlebars
42
33
  end
43
34
 
44
35
  def handlebars
45
- @js.require('handlebars/base')
36
+ @js.require('handlebars')
46
37
  end
47
38
 
48
39
  def runtime
@@ -1,3 +1,3 @@
1
1
  module Handlebars
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -13,7 +13,7 @@ describe(Handlebars::Context) do
13
13
  end
14
14
 
15
15
  it "can use any Ruby object as a context" do
16
- t.call(mock(:Object, :name => "Flipper")).should eql "Hello Flipper"
16
+ t.call(double(:Object, :name => "Flipper")).should eql "Hello Flipper"
17
17
  end
18
18
  end
19
19
 
metadata CHANGED
@@ -1,36 +1,53 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handlebars
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
5
- prerelease:
4
+ version: 0.5.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Charles Lowell
9
8
  autorequire:
10
9
  bindir: bin
11
- cert_chain: []
12
- date: 2013-02-19 00:00:00.000000000 Z
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDPjCCAiagAwIBAgIBADANBgkqhkiG9w0BAQUFADBFMRAwDgYDVQQDDAdjb3di
14
+ b3lkMRwwGgYKCZImiZPyLGQBGRYMdGhlZnJvbnRzaWRlMRMwEQYKCZImiZPyLGQB
15
+ GRYDbmV0MB4XDTEzMDEzMDIxMDYwNFoXDTE0MDEzMDIxMDYwNFowRTEQMA4GA1UE
16
+ AwwHY293Ym95ZDEcMBoGCgmSJomT8ixkARkWDHRoZWZyb250c2lkZTETMBEGCgmS
17
+ JomT8ixkARkWA25ldDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAO45
18
+ CUxpETDGYXjCCy2dMg/aIrdrTqBqQW5ZrzhHxF9EkcdmWFr0z/qMz0JSpZ3pF11Z
19
+ KYaj5PaQQpjZfLPwbuiGGkuSWi+UAac//V18xo6S4lzRBjO+gpzG9f2AOzt9b+SR
20
+ Uc8UhO7QBZ5edUDxMxw9QstD+U0YBAlzsPJbHuUOqdtxXmNQCds3ZnqTgZaIpdUy
21
+ CSejtrukSmlthxFzwgMezYQhcYxmkl+Q475JUodnI6Pjc6nja/Or8Y6cEWiLgeUa
22
+ a+efcPGLDEbwJC7TGRrvk8yassMByBEJ3XueTMzeqWFd+665ptciojYo6BvIAR0N
23
+ iLwks0x567FZyS8SqTcCAwEAAaM5MDcwCQYDVR0TBAIwADAdBgNVHQ4EFgQUxVgR
24
+ 5TUqf7Hd24ICb3g4FNbM7oYwCwYDVR0PBAQDAgSwMA0GCSqGSIb3DQEBBQUAA4IB
25
+ AQDdJj+NzZhiYXA56z0wzRUA/Fcf6CYqKB+RFRlPssDEcHTor5SnwdWgQof/gNLi
26
+ Qel1Om4zO0Shcp89jxaUqtvEdYVhmyfc0vycHmemKttNBT734yMrEJtF8Hhy+Dnz
27
+ 9CzixXLvgGaRH+mf3M0+l+zIDJJr2L+39L8cyTSSRnp/srfI8aSmJKhGshudBKoC
28
+ Ty6Gi071pwoJXvdMaE/6iPy7bUzlndYdHyYuWSKaO9N47HqQ62oEnBraglw6ghoi
29
+ UgImJlChAzCoDP9zi9tdm6jAr7ttF25R9PPYr11ILb7dYe3qUzlNlM6zJx/nb31b
30
+ IhdyRVup4qLcqYSTPsm6u7VA
31
+ -----END CERTIFICATE-----
32
+ date: 2013-09-30 00:00:00.000000000 Z
13
33
  dependencies:
14
34
  - !ruby/object:Gem::Dependency
15
35
  name: therubyracer
16
36
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
37
  requirements:
19
38
  - - ~>
20
39
  - !ruby/object:Gem::Version
21
- version: 0.11.1
40
+ version: 0.12.0
22
41
  type: :runtime
23
42
  prerelease: false
24
43
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
44
  requirements:
27
45
  - - ~>
28
46
  - !ruby/object:Gem::Version
29
- version: 0.11.1
47
+ version: 0.12.0
30
48
  - !ruby/object:Gem::Dependency
31
49
  name: commonjs
32
50
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
51
  requirements:
35
52
  - - ~>
36
53
  - !ruby/object:Gem::Version
@@ -38,31 +55,41 @@ dependencies:
38
55
  type: :runtime
39
56
  prerelease: false
40
57
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
58
  requirements:
43
59
  - - ~>
44
60
  - !ruby/object:Gem::Version
45
61
  version: 0.2.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: handlebars-source
64
+ requirement: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 1.0.12
69
+ type: :runtime
70
+ prerelease: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.0.12
46
76
  - !ruby/object:Gem::Dependency
47
77
  name: rake
48
78
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
79
  requirements:
51
- - - ! '>='
80
+ - - '>='
52
81
  - !ruby/object:Gem::Version
53
82
  version: '0'
54
83
  type: :development
55
84
  prerelease: false
56
85
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
86
  requirements:
59
- - - ! '>='
87
+ - - '>='
60
88
  - !ruby/object:Gem::Version
61
89
  version: '0'
62
90
  - !ruby/object:Gem::Dependency
63
91
  name: rspec
64
92
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
93
  requirements:
67
94
  - - ~>
68
95
  - !ruby/object:Gem::Version
@@ -70,7 +97,6 @@ dependencies:
70
97
  type: :development
71
98
  prerelease: false
72
99
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
100
  requirements:
75
101
  - - ~>
76
102
  - !ruby/object:Gem::Version
@@ -85,7 +111,6 @@ extensions: []
85
111
  extra_rdoc_files: []
86
112
  files:
87
113
  - .gitignore
88
- - .gitmodules
89
114
  - .rspec
90
115
  - Changelog.md
91
116
  - Gemfile
@@ -100,64 +125,28 @@ files:
100
125
  - lib/handlebars/version.rb
101
126
  - spec/handlebars_spec.rb
102
127
  - spike.rb
103
- - vendor/handlebars/.gitignore
104
- - vendor/handlebars/.jshintrc
105
- - vendor/handlebars/.npmignore
106
- - vendor/handlebars/.rspec
107
- - vendor/handlebars/Gemfile
108
- - vendor/handlebars/Gemfile.lock
109
- - vendor/handlebars/LICENSE
110
- - vendor/handlebars/README.markdown
111
- - vendor/handlebars/Rakefile
112
- - vendor/handlebars/bench/benchwarmer.js
113
- - vendor/handlebars/bench/handlebars.js
114
- - vendor/handlebars/bin/handlebars
115
- - vendor/handlebars/dist/handlebars.js
116
- - vendor/handlebars/dist/handlebars.runtime.js
117
- - vendor/handlebars/lib/handlebars.js
118
- - vendor/handlebars/lib/handlebars/base.js
119
- - vendor/handlebars/lib/handlebars/compiler/ast.js
120
- - vendor/handlebars/lib/handlebars/compiler/base.js
121
- - vendor/handlebars/lib/handlebars/compiler/compiler.js
122
- - vendor/handlebars/lib/handlebars/compiler/index.js
123
- - vendor/handlebars/lib/handlebars/compiler/printer.js
124
- - vendor/handlebars/lib/handlebars/compiler/visitor.js
125
- - vendor/handlebars/lib/handlebars/runtime.js
126
- - vendor/handlebars/lib/handlebars/utils.js
127
- - vendor/handlebars/package.json
128
- - vendor/handlebars/spec/acceptance_spec.rb
129
- - vendor/handlebars/spec/parser_spec.rb
130
- - vendor/handlebars/spec/qunit_spec.js
131
- - vendor/handlebars/spec/spec_helper.rb
132
- - vendor/handlebars/spec/tokenizer_spec.rb
133
- - vendor/handlebars/src/handlebars.l
134
- - vendor/handlebars/src/handlebars.yy
135
- - vendor/handlebars/src/parser-prefix.js
136
- - vendor/handlebars/src/parser-suffix.js
137
- - vendor/handlebars/lib/handlebars/compiler/parser.js
138
128
  homepage: http://github.com/cowboyd/handlebars.rb
139
129
  licenses: []
130
+ metadata: {}
140
131
  post_install_message:
141
132
  rdoc_options: []
142
133
  require_paths:
143
134
  - lib
144
135
  required_ruby_version: !ruby/object:Gem::Requirement
145
- none: false
146
136
  requirements:
147
- - - ! '>='
137
+ - - '>='
148
138
  - !ruby/object:Gem::Version
149
139
  version: '0'
150
140
  required_rubygems_version: !ruby/object:Gem::Requirement
151
- none: false
152
141
  requirements:
153
- - - ! '>='
142
+ - - '>='
154
143
  - !ruby/object:Gem::Version
155
144
  version: '0'
156
145
  requirements: []
157
146
  rubyforge_project: handlebars
158
- rubygems_version: 1.8.25
147
+ rubygems_version: 2.0.0
159
148
  signing_key:
160
- specification_version: 3
149
+ specification_version: 4
161
150
  summary: Ruby bindings for the handlebars.js templating library
162
151
  test_files:
163
152
  - spec/handlebars_spec.rb
@@ -1,3 +0,0 @@
1
- [submodule "vendor/handlebars"]
2
- path = vendor/handlebars
3
- url = https://github.com/wycats/handlebars.js.git
@@ -1,8 +0,0 @@
1
- vendor
2
- .rvmrc
3
- .DS_Store
4
- lib/handlebars/compiler/parser.js
5
- node_modules
6
- *.sublime-project
7
- *.sublime-workspace
8
- npm-debug.log
@@ -1,52 +0,0 @@
1
- {
2
- "predef": [
3
- "console",
4
- "Ember",
5
- "DS",
6
- "Handlebars",
7
- "Metamorph",
8
- "ember_assert",
9
- "ember_warn",
10
- "ember_deprecate",
11
- "ember_deprecateFunc",
12
- "require",
13
- "suite",
14
- "equal",
15
- "equals",
16
- "test",
17
- "testBoth",
18
- "raises",
19
- "deepEqual",
20
- "start",
21
- "stop",
22
- "ok",
23
- "strictEqual",
24
- "module"
25
- ],
26
-
27
- "node" : true,
28
- "es5" : true,
29
- "browser" : true,
30
-
31
- "boss" : true,
32
- "curly": false,
33
- "debug": false,
34
- "devel": false,
35
- "eqeqeq": true,
36
- "evil": true,
37
- "forin": false,
38
- "immed": false,
39
- "laxbreak": false,
40
- "newcap": true,
41
- "noarg": true,
42
- "noempty": false,
43
- "nonew": false,
44
- "nomen": false,
45
- "onevar": false,
46
- "plusplus": false,
47
- "regexp": false,
48
- "undef": true,
49
- "sub": true,
50
- "strict": false,
51
- "white": false
52
- }
@@ -1,10 +0,0 @@
1
- .DS_Store
2
- .gitignore
3
- .rvmrc
4
- Gemfile
5
- Gemfile.lock
6
- Rakefile
7
- bench/*
8
- spec/*
9
- src/*
10
- vendor/*
@@ -1 +0,0 @@
1
- -cfs
@@ -1,5 +0,0 @@
1
- source "http://rubygems.org"
2
-
3
- gem "rake"
4
- gem "therubyracer", ">= 0.9.8", "< 0.11"
5
- gem "rspec"
@@ -1,19 +0,0 @@
1
- Copyright (C) 2011 by Yehuda Katz
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy
4
- of this software and associated documentation files (the "Software"), to deal
5
- in the Software without restriction, including without limitation the rights
6
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- copies of the Software, and to permit persons to whom the Software is
8
- furnished to do so, subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in
11
- all copies or substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- THE SOFTWARE.
@@ -1,317 +0,0 @@
1
- [![Build Status](https://secure.travis-ci.org/wycats/handlebars.js.png)](http://travis-ci.org/wycats/handlebars.js)
2
-
3
- Handlebars.js
4
- =============
5
-
6
- Handlebars.js is an extension to the [Mustache templating language](http://mustache.github.com/) created by Chris Wanstrath. Handlebars.js and Mustache are both logicless templating languages that keep the view and the code separated like we all know they should be.
7
-
8
- Checkout the official Handlebars docs site at [http://www.handlebarsjs.com](http://www.handlebarsjs.com).
9
-
10
-
11
- Installing
12
- ----------
13
- Installing Handlebars is easy. Simply [download the package from GitHub](https://github.com/wycats/handlebars.js/archives/master) and add it to your web pages (you should usually use the most recent version).
14
-
15
- Usage
16
- -----
17
- In general, the syntax of Handlebars.js templates is a superset of Mustache templates. For basic syntax, check out the [Mustache manpage](http://mustache.github.com/mustache.5.html).
18
-
19
- Once you have a template, use the Handlebars.compile method to compile the template into a function. The generated function takes a context argument, which will be used to render the template.
20
-
21
- ```js
22
- var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " +
23
- "{{kids.length}} kids:</p>" +
24
- "<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>";
25
- var template = Handlebars.compile(source);
26
-
27
- var data = { "name": "Alan", "hometown": "Somewhere, TX",
28
- "kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]};
29
- var result = template(data);
30
-
31
- // Would render:
32
- // <p>Hello, my name is Alan. I am from Somewhere, TX. I have 2 kids:</p>
33
- // <ul>
34
- // <li>Jimmy is 12</li>
35
- // <li>Sally is 4</li>
36
- // </ul>
37
- ```
38
-
39
-
40
- Registering Helpers
41
- -------------------
42
-
43
- You can register helpers that Handlebars will use when evaluating your
44
- template. Here's an example, which assumes that your objects have a URL
45
- embedded in them, as well as the text for a link:
46
-
47
- ```js
48
- Handlebars.registerHelper('link_to', function(context) {
49
- return "<a href='" + context.url + "'>" + context.body + "</a>";
50
- });
51
-
52
- var context = { posts: [{url: "/hello-world", body: "Hello World!"}] };
53
- var source = "<ul>{{#posts}}<li>{{{link_to this}}}</li>{{/posts}}</ul>"
54
-
55
- var template = Handlebars.compile(source);
56
- template(context);
57
-
58
- // Would render:
59
- //
60
- // <ul>
61
- // <li><a href='/hello-world'>Hello World!</a></li>
62
- // </ul>
63
- ```
64
-
65
- Escaping
66
- --------
67
-
68
- By default, the `{{expression}}` syntax will escape its contents. This
69
- helps to protect you against accidental XSS problems caused by malicious
70
- data passed from the server as JSON.
71
-
72
- To explicitly *not* escape the contents, use the triple-mustache
73
- (`{{{}}}`). You have seen this used in the above example.
74
-
75
-
76
- Differences Between Handlebars.js and Mustache
77
- ----------------------------------------------
78
- Handlebars.js adds a couple of additional features to make writing templates easier and also changes a tiny detail of how partials work.
79
-
80
- ### Paths
81
-
82
- Handlebars.js supports an extended expression syntax that we call paths. Paths are made up of typical expressions and . characters. Expressions allow you to not only display data from the current context, but to display data from contexts that are descendents and ancestors of the current context.
83
-
84
- To display data from descendent contexts, use the `.` character. So, for example, if your data were structured like:
85
-
86
- ```js
87
- var data = {"person": { "name": "Alan" }, company: {"name": "Rad, Inc." } };
88
- ```
89
-
90
- you could display the person's name from the top-level context with the following expression:
91
-
92
- ```
93
- {{person.name}}
94
- ```
95
-
96
- You can backtrack using `../`. For example, if you've already traversed into the person object you could still display the company's name with an expression like `{{../company.name}}`, so:
97
-
98
- ```
99
- {{#person}}{{name}} - {{../company.name}}{{/person}}
100
- ```
101
-
102
- would render:
103
-
104
- ```
105
- Alan - Rad, Inc.
106
- ```
107
-
108
- ### Strings
109
-
110
- When calling a helper, you can pass paths or Strings as parameters. For
111
- instance:
112
-
113
- ```js
114
- Handlebars.registerHelper('link_to', function(title, context) {
115
- return "<a href='/posts" + context.url + "'>" + title + "!</a>"
116
- });
117
-
118
- var context = { posts: [{url: "/hello-world", body: "Hello World!"}] };
119
- var source = '<ul>{{#posts}}<li>{{{link_to "Post" this}}}</li>{{/posts}}</ul>'
120
-
121
- var template = Handlebars.compile(source);
122
- template(context);
123
-
124
- // Would render:
125
- //
126
- // <ul>
127
- // <li><a href='/posts/hello-world'>Post!</a></li>
128
- // </ul>
129
- ```
130
-
131
- When you pass a String as a parameter to a helper, the literal String
132
- gets passed to the helper function.
133
-
134
-
135
- ### Block Helpers
136
-
137
- Handlebars.js also adds the ability to define block helpers. Block helpers are functions that can be called from anywhere in the template. Here's an example:
138
-
139
- ```js
140
- var source = "<ul>{{#people}}<li>{{#link}}{{name}}{{/link}}</li>{{/people}}</ul>";
141
- Handlebars.registerHelper('link', function(options) {
142
- return '<a href="/people/' + this.id + '">' + options.fn(this) + '</a>';
143
- });
144
- var template = Handlebars.compile(source);
145
-
146
- var data = { "people": [
147
- { "name": "Alan", "id": 1 },
148
- { "name": "Yehuda", "id": 2 }
149
- ]};
150
- template(data);
151
-
152
- // Should render:
153
- // <ul>
154
- // <li><a href="/people/1">Alan</a></li>
155
- // <li><a href="/people/2">Yehuda</a></li>
156
- // </ul>
157
- ```
158
-
159
- Whenever the block helper is called it is given two parameters, the argument that is passed to the helper, or the current context if no argument is passed and the compiled contents of the block. Inside of the block helper the value of `this` is the current context, wrapped to include a method named `__get__` that helps translate paths into values within the helpers.
160
-
161
- ### Partials
162
-
163
- You can register additional templates as partials, which will be used by
164
- Handlebars when it encounters a partial (`{{> partialName}}`). Partials
165
- can either be String templates or compiled template functions. Here's an
166
- example:
167
-
168
- ```js
169
- var source = "<ul>{{#people}}<li>{{> link}}</li>{{/people}}</ul>";
170
-
171
- Handlebars.registerPartial('link', '<a href="/people/{{id}}">{{name}}</a>')
172
- var template = Handlebars.compile(source);
173
-
174
- var data = { "people": [
175
- { "name": "Alan", "id": 1 },
176
- { "name": "Yehuda", "id": 2 }
177
- ]};
178
-
179
- template(data);
180
-
181
- // Should render:
182
- // <ul>
183
- // <li><a href="/people/1">Alan</a></li>
184
- // <li><a href="/people/2">Yehuda</a></li>
185
- // </ul>
186
- ```
187
-
188
- ### Comments
189
-
190
- You can add comments to your templates with the following syntax:
191
-
192
- ```js
193
- {{! This is a comment }}
194
- ```
195
-
196
- You can also use real html comments if you want them to end up in the output.
197
-
198
- ```html
199
- <div>
200
- {{! This comment will not end up in the output }}
201
- <!-- This comment will show up in the output -->
202
- </div>
203
- ```
204
-
205
-
206
- Precompiling Templates
207
- ----------------------
208
-
209
- Handlebars allows templates to be precompiled and included as javascript
210
- code rather than the handlebars template allowing for faster startup time.
211
-
212
- ### Installation
213
- The precompiler script may be installed via npm using the `npm install -g handlebars`
214
- command.
215
-
216
- ### Usage
217
-
218
- <pre>
219
- Precompile handlebar templates.
220
- Usage: handlebars template...
221
-
222
- Options:
223
- -a, --amd Create an AMD format function (allows loading with RequireJS) [boolean]
224
- -f, --output Output File [string]
225
- -k, --known Known helpers [string]
226
- -o, --knownOnly Known helpers only [boolean]
227
- -m, --min Minimize output [boolean]
228
- -s, --simple Output template function only. [boolean]
229
- -r, --root Template root. Base value that will be stripped from template names. [string]
230
- </pre>
231
-
232
- If using the precompiler's normal mode, the resulting templates will be stored
233
- to the `Handlebars.templates` object using the relative template name sans the
234
- extension. These templates may be executed in the same manner as templates.
235
-
236
- If using the simple mode the precompiler will generate a single javascript method.
237
- To execute this method it must be passed to the using the `Handlebars.template`
238
- method and the resulting object may be as normal.
239
-
240
- ### Optimizations
241
-
242
- - Rather than using the full _handlebars.js_ library, implementations that
243
- do not need to compile templates at runtime may include _handlebars.runtime.js_
244
- whose min+gzip size is approximately 1k.
245
- - If a helper is known to exist in the target environment they may be defined
246
- using the `--known name` argument may be used to optimize accesses to these
247
- helpers for size and speed.
248
- - When all helpers are known in advance the `--knownOnly` argument may be used
249
- to optimize all block helper references.
250
-
251
-
252
- Performance
253
- -----------
254
-
255
- In a rough performance test, precompiled Handlebars.js templates (in the original version of Handlebars.js) rendered in about half the time of Mustache templates. It would be a shame if it were any other way, since they were precompiled, but the difference in architecture does have some big performance advantages. Justin Marney, a.k.a. [gotascii](http://github.com/gotascii), confirmed that with an [independent test](http://sorescode.com/2010/09/12/benchmarks.html). The rewritten Handlebars (current version) is faster than the old version, and we will have some benchmarks in the near future.
256
-
257
-
258
- Building
259
- --------
260
-
261
- To build handlebars, just run `rake release`, and you will get two files
262
- in the `dist` directory.
263
-
264
-
265
- Upgrading
266
- ---------
267
-
268
- When upgrading from the Handlebars 0.9 series, be aware that the
269
- signature for passing custom helpers or partials to templates has
270
- changed.
271
-
272
- Instead of:
273
-
274
- ```js
275
- template(context, helpers, partials, [data])
276
- ```
277
-
278
- Use:
279
-
280
- ```js
281
- template(context, {helpers: helpers, partials: partials, data: data})
282
- ```
283
-
284
- Known Issues
285
- ------------
286
- * Handlebars.js can be cryptic when there's an error while rendering.
287
- * Using a variable, helper, or partial named `class` causes errors in IE browsers. (Instead, use `className`)
288
-
289
- Handlebars in the Wild
290
- -----------------
291
- * [jblotus](http://github.com/jblotus) created [http://tryhandlebarsjs.com](http://tryhandlebarsjs.com) for anyone who would
292
- like to try out Handlebars.js in their browser.
293
- * Don Park wrote an Express.js view engine adapter for Handlebars.js called [hbs](http://github.com/donpark/hbs).
294
- * [sammy.js](http://github.com/quirkey/sammy) by Aaron Quint, a.k.a. quirkey, supports Handlebars.js as one of its template plugins.
295
- * [SproutCore](http://www.sproutcore.com) uses Handlebars.js as its main templating engine, extending it with automatic data binding support.
296
- * [Ember.js](http://www.emberjs.com) makes Handlebars.js the primary way to structure your views, also with automatic data binding support.
297
- * Les Hill (@leshill) wrote a Rails Asset Pipeline gem named [handlebars_assets](http://github.com/leshill/handlebars_assets).
298
-
299
- Helping Out
300
- -----------
301
- To build Handlebars.js you'll need a few things installed.
302
-
303
- * Node.js
304
- * Jison, for building the compiler - `npm install jison`
305
- * Ruby
306
- * therubyracer, for running tests - `gem install therubyracer`
307
- * rspec, for running tests - `gem install rspec`
308
-
309
- There's a Gemfile in the repo, so you can run `bundle` to install rspec and therubyracer if you've got bundler installed.
310
-
311
- To build Handlebars.js from scratch, you'll want to run `rake compile` in the root of the project. That will build Handlebars and output the results to the dist/ folder. To run tests, run `rake spec`. You can also run our set of benchmarks with `rake bench`.
312
-
313
- If you notice any problems, please report them to the GitHub issue tracker at [http://github.com/wycats/handlebars.js/issues](http://github.com/wycats/handlebars.js/issues). Feel free to contact commondream or wycats through GitHub with any other questions or feature requests. To submit changes fork the project and send a pull request.
314
-
315
- License
316
- -------
317
- Handlebars.js is released under the MIT license.