Skip to content

Commit d0f2945

Browse files
committed
initial commit
0 parents  commit d0f2945

File tree

9 files changed

+418
-0
lines changed

9 files changed

+418
-0
lines changed

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in tilt-handlebars.gemspec
4+
gemspec

LICENSE.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2013 Jim Cushing
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Tilt::Handlebars
2+
3+
Adds support for [Handlebars.rb](https://github.com/cowboyd/handlebars.rb) template
4+
engine to [Tilt](https://github.com/rtomayko/tilt).
5+
6+
See the [Handlebars.js](http://handlebarsjs.com) site for syntax.
7+
8+
## Installation
9+
10+
Add this line to your application's Gemfile:
11+
12+
gem 'tilt-handlebars'
13+
14+
And then execute:
15+
16+
$ bundle
17+
18+
Or install it yourself as:
19+
20+
$ gem install tilt-handlebars
21+
22+
## Usage
23+
24+
Create a Handlebars template file with either a `.hbs` or `.handlebars` extension.
25+
26+
Example, in `hello.hbs`:
27+
28+
```
29+
Hello, {{name}}. I'm {{emotion}} to meet you.
30+
```
31+
32+
Then, render the template with Ruby:
33+
34+
```ruby
35+
require 'tilt/handlebars'
36+
37+
template = Tilt.new('hello.hbs')
38+
puts template.render(nil, name: "Joe", emotion: "happy")
39+
```
40+
41+
Output:
42+
43+
Hello, Joe. I'm happy to meet you.
44+
45+
## Contributing
46+
47+
1. Fork it
48+
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

Rakefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require "bundler/gem_tasks"
2+
require 'rake/testtask'
3+
4+
desc 'Run tests (default)'
5+
Rake::TestTask.new(:test) do |t|
6+
t.test_files = FileList['test/*_test.rb']
7+
t.ruby_opts = ['-Itest']
8+
t.ruby_opts << '-rubygems' if defined? Gem
9+
end

lib/tilt/handlebars.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'tilt' unless defined? Tilt
2+
require 'handlebars'
3+
4+
module Tilt
5+
6+
# Handlebars.rb template implementation. See:
7+
# https://github.com/cowboyd/handlebars.rb
8+
# and http://handlebarsjs.com
9+
#
10+
# Handlebars is a logic-less template rendered with JavaScript.
11+
# Handlebars.rb is a Ruby wrapper around Handlebars, that allows
12+
# Handlebars templates to be rendered server side.
13+
#
14+
class HandlebarsTemplate < Template
15+
def initialize_engine
16+
@context = ::Handlebars::Context.new
17+
end
18+
19+
def self.engine_initialized?
20+
true
21+
end
22+
23+
def prepare
24+
@context = ::Handlebars::Context.new
25+
@template = @context.compile(data)
26+
end
27+
28+
def evaluate(scope, locals = {}, &block)
29+
# Based on LiquidTemplate
30+
locals = locals.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
31+
if scope.respond_to?(:to_h)
32+
scope = scope.to_h.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
33+
locals = scope.merge(locals)
34+
else
35+
scope.instance_variables.each do |var|
36+
key = var.to_s.delete("@")
37+
locals[key] = scope.instance_variable_get(var) unless locals.has_key? key
38+
end
39+
end
40+
41+
locals['yield'] = block.nil? ? '' : yield
42+
locals['content'] = locals['yield']
43+
44+
@template.call(locals);
45+
end
46+
47+
def register_helper(name, &fn)
48+
@context.register_helper(name, &fn)
49+
end
50+
51+
def register_partial(*args)
52+
@context.register_partial(*args)
53+
end
54+
55+
def partial_missing(&fn)
56+
@context.partial_missing(&fn)
57+
end
58+
59+
def allows_script?
60+
false
61+
end
62+
end
63+
64+
register HandlebarsTemplate, 'handlebars', 'hbs'
65+
end
66+
67+

test/hello.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, {{ name }}. I'm {{ emotion }} to meet you.

0 commit comments

Comments
 (0)