Change sails.js EJS views to use .html extensions instead of .ejs extensions?

怎甘沉沦 提交于 2019-12-01 17:57:27

In your config/views.js:

engine: {
  ext: 'html',
  fn: require('ejs').renderFile
},

Seems though that the future support for this feature is not guaranteed, since they removed this from docs, so use with caution.

Another approach

Sails provides EJS templating by default.To override this and to use .html files , here is a simple solution. In your Sails App , go to config/routes.js. You will see following code there

module.exports.routes = {

 /***************************************************************************
 *                                                                          *
 * Make the view located at `views/homepage.ejs` (or `views/homepage.jade`, *
 * etc. depending on your default view engine) your home page.              *
 *                                                                          *
 * (Alternatively, remove this and add an `index.html` file in your         *
 * `assets` directory)                                                      *
 *                                                                          *
 ***************************************************************************/

 '/': {
   view: 'homepage'
 }

 /***************************************************************************
 *                                                                          *
 * Custom routes here...                                                    *
 *                                                                          *
 *  If a request to a URL doesn't match any of the custom routes above, it  *
 * is matched against Sails route blueprints. See `config/blueprints.js`    *
 * for configuration options and examples.                                  *
 *                                                                          *
 ***************************************************************************/

};

Remove the route to '/' as shown below . Keep it blank

New routes.js will look like

module.exports.routes = {

   //Remove '/' :)

};

Okay !!! now it’s done you can use your HTML files in Sails app . Put your index.html in assets folder . Sails will now load views from here :)

In latest sails.js 0.11, this also valid:

engine: 'ejs',
extension: 'html',

To check how they do this, in /node_modules/sails/lib/hooks/views/configure.js:

if (typeof sails.config.views.engine === 'string') {
    var viewExt = sails.config.views.extension || sails.config.views.engine;
    sails.config.views.engine = {
        name: sails.config.views.engine,
        ext: viewExt
    };
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!