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

扶醉桌前 提交于 2019-12-21 20:48:58

问题


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


回答1:


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.




回答2:


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 :)



来源:https://stackoverflow.com/questions/23458472/sails-404-pages-wont-use-the-default-layout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!