Skip to content
This repository has been archived by the owner on Dec 2, 2023. It is now read-only.

Commit

Permalink
add include context option
Browse files Browse the repository at this point in the history
  • Loading branch information
sixertoy committed Aug 26, 2015
1 parent b5533a3 commit 13a6117
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions spec/fixtures/include_context.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include a template file with a {{variable}} variable {{#repeat 1}}{{variable}}{{/repeat}}
19 changes: 15 additions & 4 deletions spec/src/include.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(function () {
'use strict';
var //variables
helper, result,
helper, result, repeat,
cwd = process.cwd(),
defaults = {
inverse: function (args) {
Expand All @@ -21,21 +21,23 @@
src: '',
ext: ''
}
}
},
_parent: {}
},
_parent: {}
}
},
// requires
path = require('path'),
sinon = require('sinon'),
expect = require('chai').expect,
handlebars = require('handlebars'),
mustacher = require(path.join(cwd, 'src/mustacher.js')),
Repeat = require(path.join(cwd, 'src/helpers/repeat.js')),
Include = require(path.join(cwd, 'src/helpers/include.js'));

describe('equal', function () {

beforeEach(function () {
repeat = new Repeat();
helper = new Include();
});
afterEach(function () {});
Expand Down Expand Up @@ -92,6 +94,15 @@
result = helper.render(p, defaults);
expect(result.toString()).to.equal('include a template file');
});
it('render file with context', function () {
var p = path.normalize('include_context');
defaults.data.root.partials.src = 'spec/fixtures';
defaults.data.root.partials.ext = '.hbs';
repeat.register();
helper.register();
result = helper.render(p, '{"variable":"private"}', defaults);
expect(result.toString()).to.equal('include a template file with a private variable');
});
});
});

Expand Down
25 changes: 18 additions & 7 deletions src/helpers/include.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,33 @@
fs = require('fs'),
path = require('path'),
exists = require('path-exists'),
Handlebars = require('handlebars'),
handlebars = require('handlebars'),
mustacher = require('./../mustacher'),
isstring = require('lodash.isstring');
IncludeHelper = function () {};
IncludeHelper.prototype.register = function () {
Handlebars.registerHelper('$include', this.render.bind(this));
handlebars.registerHelper('$include', this.render.bind(this));
};
IncludeHelper.prototype.render = function (filepath, options) {
IncludeHelper.prototype.render = function (filepath, context, options) {
var data,
output = 'Unable to load file',
args = mustacher.hasOptions(arguments);
if (!args || args.length < 2 || !isstring(filepath)) {
throw new Error('missing arguments');
}
if(args.length < 3){
options = context;
context = {};
} else {
context = JSON.parse(context);
context = context || {};
}
// recuperation des data
data = Handlebars.createFrame(options.data || {});
data = handlebars.createFrame(options.data || {});
data = {
root: data.root,
_parent: data._parent
};
// transformation du filepath en absolut
filepath = path.join(data.root.cwd, data.root.partials.src, filepath);
filepath += data.root.partials.ext;
Expand All @@ -45,12 +56,12 @@
output = fs.readFileSync(filepath, {
encoding: 'utf8'
}).trim();
output = Handlebars.compile(output);
output = output({}, {
output = handlebars.compile(output);
output = output(context, {
data: data
});
}
return new Handlebars.SafeString(output.trim());
return new handlebars.SafeString(output.trim());
};
module.exports = IncludeHelper;
}());

0 comments on commit 13a6117

Please sign in to comment.