Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow requiring ".handlebars" files within Node.js scripts #515

Merged
merged 3 commits into from
May 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
}