Ronen Teva
Ronen Teva

Reputation: 1437

Sails: 404 pages won't use the default layout?

In sails.js, all files that are in the views folder use layout.ejs as their template.
Error pages like 404.ejs won't use layout.ejs for some reason.

I couldn't find any setting for that, can I change it?

Thanks!

A link to the repository, if needed: http://github.com/ronenteva/MySkills

Upvotes: 3

Views: 4191

Answers (2)

Jon Garbayo
Jon Garbayo

Reputation: 105

As I was on the same problem, but for the 403 error, I found your post. Unfortunately, since Sails 0.11, things have changed, and this solution doesn't work anymore...

After some work on it, the solution is very simple.
Just edit the corresponding .js file (forbidden.js, notFound.js, etc. in the api/responses folder), to add, before any return call (why not just after the res.status(), mmmh?), this :

res.locals.layout = 'myLayout';

Supposing you named your layout file myLayout.ejs.

If you prefer to use the default layout, replace 'myLayout' by false.

And here it is :)

Upvotes: 1

sgress454
sgress454

Reputation: 24958

Sails v0.10.x uses the notFound response to serve the 404 page, equivalent to calling res.notFound() in a controller action. A default response handler is provided for you in api/responses/notFound.js, but you can customize it to do anything you like, including using res.view() to serve the 404 page with a layout. The default code uses res.render rather than res.view, because the default 404.ejs has its own layout.

Docs for custom responses are here.

In Sails v0.9, you can edit the views/404.ejs file, and then config/404.js file to use res.view instead of res.render if you'd like your updated 404.ejs to use your default layout.

Upvotes: 5

Related Questions