【问题标题】:Change sails.js EJS views to use .html extensions instead of .ejs extensions?更改sails.js EJS 视图以使用.html 扩展而不是.ejs 扩展?
【发布时间】:2014-01-09 06:35:13
【问题描述】:

是否可以将sails.js 应用配置为使用.html 扩展而不是.ejs(但仍使用ejs view engine)?

sails new app 创建 ./views/home/index.ejs./views/layout.ejs

我想将扩展名更改为 .html,但保持其他一切都以相同的方式工作。

ie:我现在有./views/home/index.html./views/layout.html,并且主页仍会照常注入到布局页面中。

请问如何配置?

【问题讨论】:

  • 此更改是否提供任何速度改进?或者您想要改变这一点的原因是什么?
  • 没有速度提升,只是更喜欢使用.html扩展名来表达我的观点

标签: javascript sails.js


【解决方案1】:

在你的config/views.js

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

似乎无法保证将来对此功能的支持,因为他们已从文档中删除了此功能,因此请谨慎使用。

【讨论】:

    【解决方案2】:

    另一种方法

    Sails 默认提供 EJS 模板。要覆盖它并使用 .html 文件,这是一个简单的解决方案。在您的 Sails App 中,转到 config/routes.js。您将在那里看到以下代码

    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.                                  *
     *                                                                          *
     ***************************************************************************/
    
    };
    

    删除到'/'的路由,如下所示。留空

    新的 routes.js 看起来像

    module.exports.routes = {
    
       //Remove '/' :)
    
    };
    

    好的!!!现在完成了,您可以在 Sails 应用程序中使用您的 HTML 文件。将您的 index.html 放在 assets 文件夹中。 Sails 现在将从这里加载视图 :)

    【讨论】:

      【解决方案3】:

      在最新的sails.js 0.11中,这也是有效的:

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

      要检查他们是如何做到这一点的,在 /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
          };
      }
      

      【讨论】:

        猜你喜欢
        • 2014-03-22
        • 2018-03-20
        • 2014-04-22
        • 1970-01-01
        • 2013-04-22
        • 1970-01-01
        • 2018-02-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多