【问题标题】:How to use .html file extensions for handlebars in express?如何在 express 中使用 .html 文件扩展名作为车把?
【发布时间】:2014-03-09 03:27:18
【问题描述】:

所以我想知道如何使用 .html 扩展名而不是 .handlebars 或 .hbs 扩展名。我这样做是为了可以使用常规 html 进行开发,这样我的前端开发人员就可以在他们的 IDE 中无缝编辑文件,而无需任何额外的配置。此外,它将有助于更快地将 html 模板安装到我们的 express 应用程序中。

【问题讨论】:

    标签: node.js express handlebars.js template-engine file-extension


    【解决方案1】:

    所以我可以通过更改 app.js 文件中的三件事来做到这一点,我希望这对每个人都有帮助!

    var express  = require('express'),
        exphbr   = require('express3-handlebars'), // "express3-handlebars"
        helpers  = require('./lib/helpers'),
    
        app = express(), 
        handlebars;
    
    // Create `ExpressHandlebars` instance with a default layout.
    handlebars = exphbr.create({
        defaultLayout: 'main',
        helpers      : helpers,
        extname      : '.html', //set extension to .html so handlebars knows what to look for
    
        // Uses multiple partials dirs, templates in "shared/templates/" are shared
        // with the client-side of the app (see below).
        partialsDir: [
            'views/shared/',
            'views/partials/'
        ]
    });
    
    // Register `hbs` as our view engine using its bound `engine()` function.
    // Set html in app.engine and app.set so express knows what extension to look for.
    app.engine('html', handlebars.engine);
    app.set('view engine', 'html');
    
    // Seperate route.js file
    require("./routes")(app, express);
    
    app.listen(3000);
    

    【讨论】:

      【解决方案2】:

      同意JemiloII

      1. extname: '.myext' 在配置中同时根据https://www.npmjs.org/package/express3-handlebars#-extname-handlebars- 创建 expr-HBS 实例 (exphbr.create())
      2. expr-HBS引擎绑定到扩展:app.engine('myext', handlebars.engine);根据http://expressjs.com/3x/api.html#app.engine
      3. 将扩展设置为视图引擎:app.set('view engine', 'myext'); - 遗憾的是没有链接到它的工作原理。

      问候

      【讨论】:

      • 你知道如何使用这两种扩展吗?
      猜你喜欢
      • 2015-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多