Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added support for Handlebars helpers with test. #96

Merged
merged 1 commit into from

2 participants

@aral

Helpers are required in Handlebars when returning safe strings, etc. Added support (same as partials) and a test.

@tj tj merged commit 3fdc7eb into tj:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 8, 2013
  1. @aral

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

    aral authored
    …ed) HTML back to the template, etc. Added tests.
This page is out of date. Refresh to see the latest.
View
3  lib/consolidate.js
@@ -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
1  test/consolidate.js
@@ -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');
View
1  test/fixtures/handlebars/helpers.handlebars
@@ -0,0 +1 @@
+{{safe user.name}}
View
36 test/shared/helpers.js
@@ -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();
+ });
+ });
+ }
+ });
+};
Something went wrong with that request. Please try again.