Permalink
Browse files

Added support for Handlebars helpers. Use case: sending safe (unescap…

…ed) HTML back to the template, etc. Added tests.
  • Loading branch information...
aral committed Mar 8, 2013
1 parent 579da6b commit 979ee82c85e0880fd5a49bff9f5c2929b395257e
Showing with 41 additions and 0 deletions.
  1. +3 −0 lib/consolidate.js
  2. +1 −0 test/consolidate.js
  3. +1 −0 test/fixtures/handlebars/helpers.handlebars
  4. +36 −0 test/shared/helpers.js
View
@@ -483,6 +483,9 @@ exports.handlebars.render = function(str, options, fn) {
for (var partial in options.partials) {
engine.registerPartial(partial, options.partials[partial]);
}
+ for (var helper in options.helpers) {
+ engine.registerHelper(helper, options.helpers[helper]);
+ }
var tmpl = cache(options) || cache(options, engine.compile(str, options));
fn(null, tmpl(options));
} catch (err) {
View
@@ -15,6 +15,7 @@ require('./shared').test('dust');
require('./shared/partials').test('dust');
require('./shared').test('handlebars');
require('./shared/partials').test('handlebars');
+require('./shared/helpers').test('handlebars');
require('./shared').test('underscore');
require('./shared').test('qejs');
require('./shared').test('walrus');
@@ -0,0 +1 @@
+{{safe user.name}}
View
@@ -0,0 +1,36 @@
+
+var cons = require('../../')
+ , handlebars = require('handlebars')
+ , fs = require('fs')
+ , readFile = fs.readFile
+ , readFileSync = fs.readFileSync;
+
+exports.test = function(name) {
+ var user = { name: '<strong>Tobi</strong>' };
+
+ describe(name, function(){
+
+ afterEach(function(){
+ fs.readFile = readFile;
+ fs.readFileSync = readFileSync;
+ });
+
+ if (name == 'handlebars') {
+
+ // Use case: return safe HTML that won’t be escaped in the final render.
+ it('should support helpers', function(done) {
+ var str = fs.readFileSync('test/fixtures/' + name + '/helpers.' + name).toString();
+
+ var locals = { user: user, helpers: { safe: function(object) {
+ return new handlebars.SafeString(object);
+ }}};
+
+ cons[name].render(str, locals, function(err, html){
+ if (err) return done(err);
+ html.should.equal('<strong>Tobi</strong>');
+ done();
+ });
+ });
+ }
+ });
+};

0 comments on commit 979ee82

Please sign in to comment.