Skip to content

Commit

Permalink
Merge pull request handlebars-lang#515 from jjclark1982/master
Browse files Browse the repository at this point in the history
Allow requiring ".handlebars" files within Node.js scripts
  • Loading branch information
kpdecker committed May 15, 2013
2 parents 4cf0410 + d649b35 commit 94cc419
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ Handlebars.create = create;

module.exports = Handlebars; // instantiate an instance

// Publish a Node.js require() handler for .handlebars and .hbs files
if (require.extensions) {
var extension = function(module, filename) {
var fs = require("fs");
var templateString = fs.readFileSync(filename, "utf8");
module.exports = Handlebars.compile(templateString);
};
require.extensions[".handlebars"] = extension;
require.extensions[".hbs"] = extension;
}

// BEGIN(BROWSER)

// END(BROWSER)
Expand Down
1 change: 1 addition & 0 deletions spec/example_1.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{foo}}
1 change: 1 addition & 0 deletions spec/example_2.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, {{name}}!
24 changes: 24 additions & 0 deletions spec/qunit_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1502,3 +1502,27 @@ test('isEmpty', function() {
equal(Handlebars.Utils.isEmpty('foo'), false);
equal(Handlebars.Utils.isEmpty({bar: 1}), false);
});

if (typeof(require) !== 'undefined') {
suite('Require');

test('Load .handlebars files with require()', function() {
var template = require("./example_1");
assert.deepEqual(template, require("./example_1.handlebars"));

var expected = 'foo\n';
var result = template({foo: "foo"});

equal(result, expected);
});

test('Load .hbs files with require()', function() {
var template = require("./example_2");
assert.deepEqual(template, require("./example_2.hbs"));

var expected = 'Hello, World!\n';
var result = template({name: "World"});

equal(result, expected);
});
}

0 comments on commit 94cc419

Please sign in to comment.