Skip to content

Commit

Permalink
Merge pull request #57 from gilt/fix-52-layout-viewpath-array
Browse files Browse the repository at this point in the history
fixes #52: passing viewPath array and no layoutPath
  • Loading branch information
shellscape authored Oct 24, 2016
2 parents 4d544fe + 45e8347 commit f5baca0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ the following.
In addition to, or alternatively, you may specify a layout to render a template
into. Simply specify `{{!< layoutName }}` somewhere in your template. koa-hbs
will load your layout from `layoutsPath` if defined, or from `viewPath`
otherwise.
otherwise. If `viewPath` is set to an Array of paths, **_the first path in the
array will be assumed to contain the layout named._**

At this time, only a single content block (`{{{body}}}`) is supported.

Expand Down
21 changes: 18 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var util = require('util');

/**
* Shallow copy two objects into a new object
Expand Down Expand Up @@ -58,7 +59,21 @@ function MissingTemplateError (message, extra) {
this.extra = extra;
};

require('util').inherits(MissingTemplateError, Error);
util.inherits(MissingTemplateError, Error);

/**
* @class BadOptionsError
* @param {String} message The error message
* @param {Object} extra Misc infomration.
*/
function BadOptionsError (message, extra) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.extra = extra;
};

util.inherits(BadOptionsError, Error);

/**
* expose default instance of `Hbs`
Expand Down Expand Up @@ -100,7 +115,7 @@ Hbs.prototype.configure = function (options) {

var self = this;

if(!options.viewPath) { throw new Error('must specify view path'); }
if(!options.viewPath) { throw new BadOptionsError('The option `viewPath` must be specified.'); }

// Attach options
options = options || {};
Expand Down Expand Up @@ -241,7 +256,7 @@ Hbs.prototype.getLayoutPath = function(layout) {
return path.join(this.layoutsPath, layout + this.extname);
}

return path.join(this.viewPath, layout + this.extname);
return path.join(this.viewPath[0], layout + this.extname);
};

/**
Expand Down

0 comments on commit f5baca0

Please sign in to comment.