Skip to content

Commit

Permalink
Closes hapijs#846
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed May 8, 2013
1 parent 2140e91 commit 611325d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ When creating a server instance, the following options configure the server's be
directory.
- `partialsPath` - the root file path where partials are located. Partials are small segments of template code that can be nested and reused
throughout other templates. Defaults to no partials support (empty path).
- `helpersPath` - the directory path where helpers are located. Helpers are functions used within templates to perform transformations
and other data manipulations using the template context or other inputs. Each '.js' file in the helpers directory is loaded and the file name
is used as the helper name. The files must export a single method with the signature `function(context)` and return a string. Sub-folders are
not supported and are ignored. Defaults to no helpers support (empty path).
- `basePath` - a base path used as prefix for `path` and `partialsPath`. No default.
- `layout` - if set to `true`, layout support is enabled. A layout is a single template file used as the parent template for other view templates
in the same engine. The layout template name must be 'layout.ext' where 'ext' is the engine's extension.
Expand Down
2 changes: 2 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ internals.serverSchema = {
allowAbsolutePaths: T.Boolean(),
allowInsecureAccess: T.Boolean(),
partialsPath: T.String(),
helpersPath: T.String(),
contentType: T.String()
}).nullOk(),
maxSockets: T.Number().nullOk()
Expand Down Expand Up @@ -196,6 +197,7 @@ internals.viewSchema = {
allowAbsolutePaths: T.Boolean(),
allowInsecureAccess: T.Boolean(),
partialsPath: T.String().emptyOk(),
helpersPath: T.String().emptyOk(),
contentType: T.String(),
compileMode: T.String().valid('sync', 'async')
};
32 changes: 31 additions & 1 deletion lib/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ exports = module.exports = internals.Manager = function (options) {
engine.cache = {};
}

// Load partials
// Load partials and helpers

self._loadPartials(engine);
self._loadHelpers(engine);

// Set engine

Expand Down Expand Up @@ -135,6 +136,35 @@ internals.Manager.prototype._loadPartials = function (engine) {
};


internals.Manager.prototype._loadHelpers = function (engine) {

var self = this;

if (!engine.config.helpersPath ||
!engine.module.hasOwnProperty('registerHelper')) {

return;
}

var files = [];

var path = Path.join(engine.config.basePath || '', engine.config.helpersPath);
Fs.readdirSync(path).forEach(function (file) {

file = Path.join(path, file);
var stat = Fs.statSync(file);
if (stat.isFile() &&
Path.basename(file)[0] !== '.' &&
Path.extname(file) === '.js') {

var offset = path.slice(-1) === '/' ? 0 : 1;
var name = file.slice(path.length + offset, -3);
engine.module.registerHelper(name, require(file));
}
});
};


internals.Manager.prototype.render = function (filename, context, options, callback) {

var self = this;
Expand Down
4 changes: 4 additions & 0 deletions test/unit/templates/valid/helpers/uppercase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = function (context) {

return context.toUpperCase();
};
1 change: 1 addition & 0 deletions test/unit/templates/valid/testHelpers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>This is all {{uppercase this.something}} and this is how we like it!</p>
18 changes: 17 additions & 1 deletion test/unit/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,12 @@ describe('Views', function () {
});
});

it('should skip loading partial if engine does not have registerPartial method', function (done) {
it('should skip loading partials and helpers if engine does not support them', function (done) {

var tempView = new Views({
path: viewsPath + '/valid',
partialsPath: viewsPath + '/valid/partials',
helpersPath: viewsPath + '/valid/helpers',
engines: { 'html': 'jade' }
});

Expand All @@ -203,6 +204,21 @@ describe('Views', function () {
done();
});
});

it('should load helpers and render them', function (done) {

var tempView = new Views({
engines: { 'html': 'handlebars' },
path: viewsPath + '/valid',
helpersPath: viewsPath + '/valid/helpers'
});

tempView.render('testHelpers', { something: 'uppercase' }, function (err, rendered, config) {

expect(rendered).to.equal('<p>This is all UPPERCASE and this is how we like it!</p>');
done();
});
});
});

describe('#handler', function () {
Expand Down

0 comments on commit 611325d

Please sign in to comment.