handlebars 0.0.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -1
- data/.gitmodules +3 -0
- data/.rspec +1 -0
- data/Gemfile +1 -1
- data/README.mdown +44 -0
- data/Rakefile +3 -0
- data/handlebars.gemspec +19 -13
- data/lib/handlebars.rb +4 -3
- data/lib/handlebars/context.rb +37 -0
- data/lib/handlebars/version.rb +1 -1
- data/spec/handlebars_spec.rb +40 -0
- data/spike.rb +17 -0
- data/vendor/handlebars/.gitignore +6 -0
- data/vendor/handlebars/.jshintrc +50 -0
- data/vendor/handlebars/.npmignore +11 -0
- data/vendor/handlebars/Gemfile +5 -0
- data/vendor/handlebars/LICENSE +20 -0
- data/vendor/handlebars/README.markdown +315 -0
- data/vendor/handlebars/Rakefile +116 -0
- data/vendor/handlebars/bench/benchwarmer.js +149 -0
- data/vendor/handlebars/bench/handlebars.js +163 -0
- data/vendor/handlebars/bin/handlebars +139 -0
- data/vendor/handlebars/lib/handlebars.js +14 -0
- data/vendor/handlebars/lib/handlebars/base.js +101 -0
- data/vendor/handlebars/lib/handlebars/compiler/ast.js +103 -0
- data/vendor/handlebars/lib/handlebars/compiler/base.js +27 -0
- data/vendor/handlebars/lib/handlebars/compiler/compiler.js +808 -0
- data/vendor/handlebars/lib/handlebars/compiler/index.js +7 -0
- data/vendor/handlebars/lib/handlebars/compiler/printer.js +137 -0
- data/vendor/handlebars/lib/handlebars/compiler/visitor.js +13 -0
- data/vendor/handlebars/lib/handlebars/runtime.js +68 -0
- data/vendor/handlebars/lib/handlebars/utils.js +68 -0
- data/vendor/handlebars/package.json +25 -0
- data/vendor/handlebars/spec/acceptance_spec.rb +101 -0
- data/vendor/handlebars/spec/parser_spec.rb +264 -0
- data/vendor/handlebars/spec/qunit_spec.js +1067 -0
- data/vendor/handlebars/spec/spec_helper.rb +157 -0
- data/vendor/handlebars/spec/tokenizer_spec.rb +254 -0
- data/vendor/handlebars/src/handlebars.l +42 -0
- data/vendor/handlebars/src/handlebars.yy +99 -0
- metadata +93 -77
- data/README.md +0 -39
- data/lib/handlebars/generator.rb +0 -4
- data/lib/handlebars/parser.rb +0 -240
- data/spec/generator_spec.rb +0 -5
- data/spec/parser_spec.rb +0 -163
- data/spec/spec_helper.rb +0 -17
data/.gitignore
CHANGED
data/.gitmodules
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/Gemfile
CHANGED
data/README.mdown
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
|
2
|
+
## Handlebars.rb
|
3
|
+
|
4
|
+
This uses [therubyracer][1] to bind to the _actual_ JavaScript implementation of
|
5
|
+
[Handlebars.js][2] so that you can use it from ruby.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
Simple stuff
|
10
|
+
|
11
|
+
require 'handlebars'
|
12
|
+
template = Handlebars.compile("{{say}}{{what}}")
|
13
|
+
template.call(:say => "Hey", :what => "Yuh!") #=> "Hey Yuh!"
|
14
|
+
|
15
|
+
With functions as properties
|
16
|
+
|
17
|
+
template.call(:say => "Hey", :what => lambda {|this| ("yo" * 2) + "!"}) #=> "Hey yoyo!"
|
18
|
+
|
19
|
+
Register block helpers:
|
20
|
+
|
21
|
+
Handlebars.register_helper(:twice) do |block|
|
22
|
+
"#{block.call}#{block.call}"
|
23
|
+
end
|
24
|
+
template = Handlebars.compile({{#twice}}Hurray!{{/twice}})
|
25
|
+
template.call #=> Hurray!Hurray!
|
26
|
+
|
27
|
+
## Hack
|
28
|
+
|
29
|
+
git clone git@github.com:cowboyd/handlebars.rb.git #git it
|
30
|
+
cd handlebars.rb #go to it
|
31
|
+
git submodule update --init #pull down handlebars.js
|
32
|
+
rspec spec/ #test it
|
33
|
+
|
34
|
+
|
35
|
+
## Rubygems
|
36
|
+
|
37
|
+
Handlebars is available via the [hbs][3] gem on rubygems.org. There is also a
|
38
|
+
[handlebars][4] gem, which is a pure-ruby implementation that appears to no longer be maintained.
|
39
|
+
|
40
|
+
[1]: http://github.com/cowboyd/therubyracer "The Ruby Racer"
|
41
|
+
[2]: http://github.com/wycats/handlebars.js "Handlebars JavaScript templating library"
|
42
|
+
[3]: http://rubygems.org/gems/hbs "handelbars gem in JavaScript"
|
43
|
+
[4]: http://rubygems.org/gems/handlebars "pure ruby handlebars gem"
|
44
|
+
|
data/Rakefile
CHANGED
data/handlebars.gemspec
CHANGED
@@ -1,23 +1,29 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "handlebars/version"
|
3
4
|
|
4
5
|
Gem::Specification.new do |s|
|
5
6
|
s.name = "handlebars"
|
6
7
|
s.version = Handlebars::VERSION
|
7
8
|
s.platform = Gem::Platform::RUBY
|
8
|
-
s.authors = [
|
9
|
-
s.email = [
|
10
|
-
s.homepage = "http://github.com/
|
11
|
-
s.summary =
|
12
|
-
s.description =
|
9
|
+
s.authors = ["Charles Lowell"]
|
10
|
+
s.email = ["cowboyd@thefrontside.net"]
|
11
|
+
s.homepage = "http://github.com/cowboyd/handlebars.rb"
|
12
|
+
s.summary = %q{Ruby bindings for the handlebars.js templating library}
|
13
|
+
s.description = %q{Uses the actual JavaScript implementation of Handlebars, but supports using Ruby objects as template contexts and Ruby procs as view functions and named helpers}
|
13
14
|
|
14
|
-
s.
|
15
|
-
s.rubyforge_project = "handlebars"
|
15
|
+
s.rubyforge_project = "handlebars"
|
16
16
|
|
17
|
-
s.
|
18
|
-
s.
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
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
|
19
25
|
|
20
|
-
s.
|
21
|
-
s.
|
22
|
-
s.
|
26
|
+
s.add_dependency "therubyracer", "~> 0.10.0beta1"
|
27
|
+
s.add_dependency "commonjs", "~> 0.2.3"
|
28
|
+
s.add_development_dependency "rspec", "~> 2.0"
|
23
29
|
end
|
data/lib/handlebars.rb
CHANGED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'commonjs'
|
2
|
+
|
3
|
+
module Handlebars
|
4
|
+
class Context
|
5
|
+
def initialize
|
6
|
+
@js = CommonJS::Environment.new V8::Context.new, :path => [
|
7
|
+
File.expand_path('../../../vendor/bootstrap', __FILE__),
|
8
|
+
File.expand_path('../../../vendor/handlebars/lib', __FILE__)
|
9
|
+
]
|
10
|
+
|
11
|
+
# This is a slightly modified version of handlebars.js found in the main
|
12
|
+
# distribution. The Ruby commonjs environment does not support full directory
|
13
|
+
# requires, so we expand them by hand. Eventually this may be fixed upstream
|
14
|
+
# but right now I'm not sure if this is a node-specific extension.
|
15
|
+
|
16
|
+
@js.require('handlebars/base')
|
17
|
+
@js.require('handlebars/utils')
|
18
|
+
for compiler_module in %w(ast base compiler index parser printer visitor)
|
19
|
+
@js.require("handlebars/compiler/#{compiler_module}")
|
20
|
+
end
|
21
|
+
@js.require('handlebars/runtime')
|
22
|
+
end
|
23
|
+
|
24
|
+
def compile(*args)
|
25
|
+
handlebars.compile(*args)
|
26
|
+
end
|
27
|
+
|
28
|
+
def register_helper(name, &fn)
|
29
|
+
handlebars.registerHelper(name, fn)
|
30
|
+
end
|
31
|
+
|
32
|
+
def handlebars
|
33
|
+
@js.require('handlebars/base')
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
data/lib/handlebars/version.rb
CHANGED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
require 'handlebars'
|
3
|
+
describe(Handlebars::Context) do
|
4
|
+
|
5
|
+
describe "a simple template" do
|
6
|
+
let(:t) {subject.compile("Hello {{name}}")}
|
7
|
+
it "allows simple subsitution" do
|
8
|
+
t.call(:name => 'World').should eql "Hello World"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "allows Ruby blocks as a property" do
|
12
|
+
t.call(:name => lambda {|context| ;"Mate"}).should eql "Hello Mate"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "can use any Ruby object as a context" do
|
16
|
+
t.call(mock(:Object, :name => "Flipper")).should eql "Hello Flipper"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "registering Helpers" do
|
21
|
+
before do
|
22
|
+
subject.register_helper('alsowith') do |this, context, block|
|
23
|
+
block.call(context)
|
24
|
+
end
|
25
|
+
subject.register_helper(:twice) do |this, block|
|
26
|
+
"#{block.call}#{block.call}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "correctly passes context and implementation" do
|
31
|
+
t = subject.compile("it's so {{#alsowith weather}}*{{summary}}*{{/alsowith}}!")
|
32
|
+
t.call(:weather => {:summary => "sunny"}).should eql "it's so *sunny*!"
|
33
|
+
end
|
34
|
+
|
35
|
+
it "doesn't nee a context or arguments to the call" do
|
36
|
+
t = subject.compile("{{#twice}}Hurray!{{/twice}}")
|
37
|
+
t.call.should eql "Hurray!Hurray!"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spike.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'handlebars'
|
2
|
+
|
3
|
+
Handlebars.register_helper "table" do |block|
|
4
|
+
"<table>#{block.call}</table>"
|
5
|
+
end
|
6
|
+
|
7
|
+
Handlebars.register_helper "row" do |block|
|
8
|
+
"<tr class='awesome-row'>#{block.call}</tr>"
|
9
|
+
end
|
10
|
+
|
11
|
+
t = Handlebars.compile <<-HBS
|
12
|
+
{{#table width}}
|
13
|
+
{{#row}}<td>Hi</td>{{/row}}
|
14
|
+
{{/table}}
|
15
|
+
HBS
|
16
|
+
|
17
|
+
puts t.call
|
@@ -0,0 +1,50 @@
|
|
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
|
+
"equal",
|
14
|
+
"test",
|
15
|
+
"testBoth",
|
16
|
+
"raises",
|
17
|
+
"deepEqual",
|
18
|
+
"start",
|
19
|
+
"stop",
|
20
|
+
"ok",
|
21
|
+
"strictEqual",
|
22
|
+
"module"
|
23
|
+
],
|
24
|
+
|
25
|
+
"node" : true,
|
26
|
+
"es5" : true,
|
27
|
+
"browser" : true,
|
28
|
+
|
29
|
+
"boss" : true,
|
30
|
+
"curly": false,
|
31
|
+
"debug": false,
|
32
|
+
"devel": false,
|
33
|
+
"eqeqeq": true,
|
34
|
+
"evil": true,
|
35
|
+
"forin": false,
|
36
|
+
"immed": false,
|
37
|
+
"laxbreak": false,
|
38
|
+
"newcap": true,
|
39
|
+
"noarg": true,
|
40
|
+
"noempty": false,
|
41
|
+
"nonew": false,
|
42
|
+
"nomen": false,
|
43
|
+
"onevar": false,
|
44
|
+
"plusplus": false,
|
45
|
+
"regexp": false,
|
46
|
+
"undef": true,
|
47
|
+
"sub": true,
|
48
|
+
"strict": false,
|
49
|
+
"white": false
|
50
|
+
}
|
@@ -0,0 +1,20 @@
|
|
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.
|
20
|
+
|
@@ -0,0 +1,315 @@
|
|
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.id + "'>" + 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='/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(context, fn) {
|
142
|
+
return '<a href="/people/' + this.__get__("id") + '">' + 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
|
+
-f, --output Output File [string]
|
224
|
+
-k, --known Known helpers [string]
|
225
|
+
-o, --knownOnly Known helpers only [boolean]
|
226
|
+
-m, --min Minimize output [boolean]
|
227
|
+
-s, --simple Output template function only. [boolean]
|
228
|
+
-r, --root Template root. Base value that will be stripped from template names. [string]
|
229
|
+
</pre>
|
230
|
+
|
231
|
+
If using the precompiler's normal mode, the resulting templates will be stored
|
232
|
+
to the `Handlebars.templates` object using the relative template name sans the
|
233
|
+
extension. These templates may be executed in the same manner as templates.
|
234
|
+
|
235
|
+
If using the simple mode the precompiler will generate a single javascript method.
|
236
|
+
To execute this method it must be passed to the using the `Handlebars.template`
|
237
|
+
method and the resulting object may be as normal.
|
238
|
+
|
239
|
+
### Optimizations
|
240
|
+
|
241
|
+
- Rather than using the full _handlebars.js_ library, implementations that
|
242
|
+
do not need to compile templates at runtime may include _handlebars.runtime.js_
|
243
|
+
whose min+gzip size is approximately 1k.
|
244
|
+
- If a helper is known to exist in the target environment they may be defined
|
245
|
+
using the `--known name` argument may be used to optimize accesses to these
|
246
|
+
helpers for size and speed.
|
247
|
+
- When all helpers are known in advance the `--knownOnly` argument may be used
|
248
|
+
to optimize all block helper references.
|
249
|
+
|
250
|
+
|
251
|
+
Performance
|
252
|
+
-----------
|
253
|
+
|
254
|
+
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.
|
255
|
+
|
256
|
+
|
257
|
+
Building
|
258
|
+
--------
|
259
|
+
|
260
|
+
To build handlebars, just run `rake release`, and you will get two files
|
261
|
+
in the `dist` directory.
|
262
|
+
|
263
|
+
|
264
|
+
Upgrading
|
265
|
+
---------
|
266
|
+
|
267
|
+
When upgrading from the Handlebars 0.9 series, be aware that the
|
268
|
+
signature for passing custom helpers or partials to templates has
|
269
|
+
changed.
|
270
|
+
|
271
|
+
Instead of:
|
272
|
+
|
273
|
+
```js
|
274
|
+
template(context, helpers, partials, [data])
|
275
|
+
```
|
276
|
+
|
277
|
+
Use:
|
278
|
+
|
279
|
+
```js
|
280
|
+
template(context, {helpers: helpers, partials: partials, data: data})
|
281
|
+
```
|
282
|
+
|
283
|
+
Known Issues
|
284
|
+
------------
|
285
|
+
* Handlebars.js can be cryptic when there's an error while rendering.
|
286
|
+
|
287
|
+
Handlebars in the Wild
|
288
|
+
-----------------
|
289
|
+
* [jblotus](http://github.com/jblotus) created [http://tryhandlebarsjs.com](http://tryhandlebarsjs.com) for anyone who would
|
290
|
+
like to try out Handlebars.js in their browser.
|
291
|
+
* Don Park wrote an Express.js view engine adapter for Handlebars.js called [hbs](http://github.com/donpark/hbs).
|
292
|
+
* [sammy.js](http://github.com/quirkey/sammy) by Aaron Quint, a.k.a. quirkey, supports Handlebars.js as one of its template plugins.
|
293
|
+
* [SproutCore](http://www.sproutcore.com) uses Handlebars.js as its main templating engine, extending it with automatic data binding support.
|
294
|
+
* [Ember.js](http://www.emberjs.com) makes Handlebars.js the primary way to structure your views, also with automatic data binding support.
|
295
|
+
* Les Hill (@leshill) wrote a Rails Asset Pipeline gem named [handlebars_assets](http://github.com/leshill/handlebars_assets).
|
296
|
+
|
297
|
+
Helping Out
|
298
|
+
-----------
|
299
|
+
To build Handlebars.js you'll need a few things installed.
|
300
|
+
|
301
|
+
* Node.js
|
302
|
+
* Jison, for building the compiler - `npm install jison`
|
303
|
+
* Ruby
|
304
|
+
* therubyracer, for running tests - `gem install therubyracer`
|
305
|
+
* rspec, for running tests - `gem install rspec`
|
306
|
+
|
307
|
+
There's a Gemfile in the repo, so you can run `bundle` to install rspec and therubyracer if you've got bundler installed.
|
308
|
+
|
309
|
+
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`.
|
310
|
+
|
311
|
+
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.
|
312
|
+
|
313
|
+
License
|
314
|
+
-------
|
315
|
+
Handlebars.js is released under the MIT license.
|