Skip to content

Commit

Permalink
Merge pull request #30 from dalthon/master
Browse files Browse the repository at this point in the history
Loads helpers written in pure javascript files
  • Loading branch information
hypomodern committed Mar 20, 2015
2 parents 889c6cf + b8a8fb8 commit 1b574e1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/handlebars/context.rb
Expand Up @@ -14,6 +14,14 @@ def compile(*args)
::Handlebars::Template.new(self, handlebars.compile(*args))
end

def load_helpers(helpers_pattern)
Dir[helpers_pattern].each{ |path| load_helper(path) }
end

def load_helper(path)
@js.load(path)
end

def precompile(*args)
handlebars.precompile(*args)
end
Expand Down
11 changes: 11 additions & 0 deletions spec/handlebars_spec.rb
Expand Up @@ -23,6 +23,17 @@
end
end

describe "loading Helpers" do
before do
subject.load_helper('spec/sample_helper.js')
end

it "can call helpers defined in a javascript file" do
t = compile('{{#nthTimes 2}}yep {{/nthTimes}}hurrah!')
t.call.should eql 'yep yep hurrah!'
end
end

describe "registering Helpers" do
before do
subject.register_helper('alsowith') do |this, context, block|
Expand Down
9 changes: 9 additions & 0 deletions spec/sample_helper.js
@@ -0,0 +1,9 @@
Handlebars.registerHelper("nthTimes", function(n, options){
var buffer = "";

for(var i = 0; i < n; i++) {
buffer += options.fn();
}

return buffer;
});

0 comments on commit 1b574e1

Please sign in to comment.