如何改变快递的车把延伸?

时间:2016-01-03 19:24:03

标签: node.js express handlebars.js

我是node.js的新手,我想使用把手模板引擎,但缩短了扩展名hbs

以下是原始代码(source):

var handlebars = require('express3-handlebars')
.create({ defaultLayout: 'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

对于带有handlebars扩展名的模板,它运行良好,但我将其更改为:

var handlebars = require('express3-handlebars')
.create({ defaultLayout: 'main' });
app.engine('hbs', handlebars.engine);
app.set('view engine', 'hbs');

并将所有模板扩展名更改为hbs。 但是,现在我收到了这个错误:

Error: ENOENT: no such file or directory, open '/path/to/node/myproject/views/layouts/main.handlebars'
   at Error (native)

我也试过

var handlebars = require('express3-handlebars')
.create({ defaultLayout: 'main' , extname : '.hbs'});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

根据答案here

但我的结果是:

Error: Failed to lookup view "500" in views directory "/path/to/myproject/views"
   at EventEmitter.render (/path/to/myproject/node_modules/express/lib/application.js:579:17)
   at ServerResponse.render (/path/to/myproject/node_modules/express/lib/response.js:961:7)
   at /path/to/myproject/app.js:96:6
   at Layer.handle_error (/path/to/myproject/node_modules/express/lib/router/layer.js:71:5)
   at trim_prefix (/path/to/myproject/node_modules/express/lib/router/index.js:310:13)
   at /path/to/myproject/node_modules/express/lib/router/index.js:280:7
   at Function.process_params (/path/to/myproject/node_modules/express/lib/router/index.js:330:12)
   at next (/path/to/myproject/node_modules/express/lib/router/index.js:271:10)
   at Layer.handle_error (/path/to/myproject/node_modules/express/lib/router/layer.js:67:12)
   at trim_prefix (/path/to/myproject/node_modules/express/lib/router/index.js:310:13)

我也尝试了其他的东西,但没有一个工作,所以想知道如何解决这个问题?

4 个答案:

答案 0 :(得分:8)

这就是诀窍:

exphbs = require('express3-handlebars'),
app.engine('hbs', exphbs({defaultLayout: 'main', extname: '.hbs'}));
app.set('view engine', 'hbs');

答案 1 :(得分:2)

您可以使用express-hbs代替import java.io.File import scala.sys.process._ "cat" #< new File("file.in") !

简单地说,你可以这样做:

express3-handlebars

答案 2 :(得分:0)

'express3-handlebars'软件包现在(2020年)已弃用。所以你可以尝试一下

const handlebarOptions = {
    viewEngine: {
      extName: '.hbs',
      partialsDir: 'views',//your path, views is a folder inside the source folder
      layoutsDir: 'views',
      defaultLayout: ''//set this one empty and provide your template below,
    },
    viewPath: 'views',
    extName: '.hbs',
  };


mailTransport.use('compile', hbs(handlebarOptions));

const mailOptions = {
        from: '"Spammy Corp." <ad.timely@gmail.com>',
        to: 'nipunkavishka@gmail.com',
        template: 'index',//this is the template of your hbs file
  };

答案 3 :(得分:0)

您的代码没有任何问题,您只需要在 views 文件夹中创建一个文件夹并将其命名为 layouts,然后移动您的 defaultLayout = main.hbs 给它。

如果你有: Error : The partial header could no be found . 再次在 views 文件夹中创建另一个文件并将其命名为 partials,然后将所有部分放在那里 __像头文件等等......。