Skip to content

Commit

Permalink
Merge pull request #245 from JaylanChen/bug-fix-#244
Browse files Browse the repository at this point in the history
bug fix #244
  • Loading branch information
sahat committed Feb 24, 2019
2 parents 5d27bb5 + b0e1e62 commit 9e6df75
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 7 additions & 5 deletions README.md
Expand Up @@ -185,7 +185,7 @@ process.env.NODE_ENV === "production"

A layout is simply a Handlebars template with a `{{{body}}}` placeholder. Usually it will be an HTML page wrapper into which views will be rendered.

This view engine adds back the concept of "layout", which was removed in Express 3.x. It can be configured with a path to the layouts directory, by default it's set to `"views/layouts/"`.
This view engine adds back the concept of "layout", which was removed in Express 3.x. It can be configured with a path to the layouts directory, by default it's set to relative to `express settings.view` + `layouts/`

There are two ways to set a default layout: configuring the view engine's `defaultLayout` property, or setting [Express locals][] `app.locals.layout`.

Expand Down Expand Up @@ -337,19 +337,21 @@ app.set('view engine', '.hbs');

**Note:** Setting the app's `"view engine"` setting will make that value the default file extension used for looking up views.

#### `layoutsDir="views/layouts/"`
#### `layoutsDir`
Default layouts directory is relative to `express settings.view` + `layouts/`
The string path to the directory where the layout templates reside.

**Note:** If you configure Express to look for views in a custom location (e.g., `app.set('views', 'some/path/')`), you will need to reflect that by passing an updated path as the `layoutsDir` property in your configuration.
**Note:** If you configure Express to look for views in a custom location (e.g., `app.set('views', 'some/path/')`), and if your `partialsDir` is not relative to `express settings.view` + `layouts/`, you will need to reflect that by passing an updated path as the `layoutsDir` property in your configuration.

#### `partialsDir="views/partials/"`
#### `partialsDir`
Default partials directory is relative to `express settings.view` + `partials/`
The string path to the directory where the partials templates reside or object with the following properties:

* `dir`: The string path to the directory where the partials templates reside.
* `namespace`: Optional string namespace to prefix the partial names.
* `templates`: Optional collection (or promise of a collection) of templates in the form: `{filename: template}`.

**Note:** If you configure Express to look for views in a custom location (e.g., `app.set('views', 'some/path/')`), you will need to reflect that by passing an updated path as the `partialsDir` property in your configuration.
**Note:** If you configure Express to look for views in a custom location (e.g., `app.set('views', 'some/path/')`), and if your `partialsDir` is not relative to `express settings.view` + `partials/`, you will need to reflect that by passing an updated path as the `partialsDir` property in your configuration.

**Note:** Multiple partials dirs can be used by making `partialsDir` an array of strings, and/or config objects as described above. The namespacing feature is useful if multiple partials dirs are used and their file paths might clash.

Expand Down
8 changes: 4 additions & 4 deletions lib/express-handlebars.js
Expand Up @@ -24,8 +24,8 @@ function ExpressHandlebars(config) {
utils.assign(this, {
handlebars : Handlebars,
extname : '.handlebars',
layoutsDir : 'views/layouts/',
partialsDir : 'views/partials/',
layoutsDir : undefined, // Default layouts directory is relative to `express settings.view` + `layouts/`
partialsDir : undefined, // Default partials directory is relative to `express settings.view` + `partials/`

This comment has been minimized.

Copy link
@dotkam

dotkam Feb 28, 2019

For services only using layoutsDir, setting partialsDir to undefined totally breaks handlebars. The throw is on line 71 below.

Line 54, partialsDirs === [undefined], then typeof dir === 'undefined', then dirPath is undefined...

Line 51 could be: if (! this.partialsDir) return;

defaultLayout : undefined,
helpers : undefined,
compilerOptions: undefined,
Expand Down Expand Up @@ -191,8 +191,8 @@ ExpressHandlebars.prototype.renderView = function (viewPath, options, callback)
var viewsPath = options.settings && options.settings.views;
if (viewsPath) {
view = this._getTemplateName(path.relative(viewsPath, viewPath));
this.partialsDir = path.join(viewsPath, 'partials/');
this.layoutsDir = path.join(viewsPath, 'layouts/');
this.partialsDir = this.partialsDir || path.join(viewsPath, 'partials/');
this.layoutsDir = this.layoutsDir || path.join(viewsPath, 'layouts/');
}

// Merge render-level and instance-level helpers together.
Expand Down

0 comments on commit 9e6df75

Please sign in to comment.