简体   繁体   English

express-handlebars 没有找到我的布局或部分

[英]express-handlebars isn't finding my layouts or partials

I have the following Express 4 view engine setup:我有以下 Express 4 视图引擎设置:

var handlebars = require('express-handlebars').create({
  layoutsDir: path.join(__dirname, "views/layouts"),
  partialsDir: path.join(__dirname, "views/partials"),
  defaultLayout: 'layout',
  extname: 'hbs'
});

app.engine('handlebars', handlebars.engine);
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, "views"));

I have the following file structure:我有以下文件结构:

/views
    error.hbs
    index.hbs
    /partials
        menu.hbs
    /layouts
        layout.hbs

And finally in my route: res.render('index');最后在我的路线中: res.render('index');

And visiting my site, it loads only my index.hbs template.访问我的网站时,它只加载我的index.hbs模板。 It does not use my layout.hbs .它不使用我的layout.hbs I don't get any errors or anything.我没有得到任何错误或任何东西。 It just doesn't use the layout even though layout is set as my default layout in the handlebars config.即使在把手配置中将layout设置为我的默认布局,它也只是不使用布局。

Next I tried to change my code to this:接下来我尝试将我的代码更改为:

res.render('index', {layout: 'layout'});

Now I get the error:现在我得到错误:

Error: ENOENT: no such file or directory, open '/.../views/layout.hbs'

So it's like it's not finding my layoutsDir ... What am I missing here?所以它就像没有找到我的layoutsDir ......我在这里错过了什么?

Next I changed it to this:接下来我把它改成了这样:

res.render('index', {layout: 'layouts/layout'});

Okay so that worked.好的,这样就行了。 My layout is now loaded.我的布局现在已加载。 But then I added in a partial to my layout:但后来我在我的布局中添加了一部分:

{{> menu }}

Now I get: /.../views/index.hbs: The partial menu could not be found现在我得到: /.../views/index.hbs: The partial menu could not be found

So what is going on here?那么这里发生了什么? How come Handlebars isn't recognizing my layoutsDir or partialsDir ?为什么 Handlebars 无法识别我的layoutsDirpartialsDir It's just not seeing them at all or something.它只是根本没有看到它们或其他东西。 And how come defaultLayout wasn't being used?为什么defaultLayout没有被使用? I had to specify the layout.我必须指定布局。

According to the API documentation for express-handlebars if you are changing the file extension from the default .handlebars then when you set the view engine, all occurrences of handlebars should be replaced with the new extension you wish to use.根据 express-handlebars 的API 文档,如果您要更改默认的.handlebars文件扩展名,那么当您设置视图引擎时,所有出现的把手都应替换为您希望使用的新扩展名。

So your express setup will need to be updated to:因此,您的快速设置需要更新为:

app.engine('hbs', handlebars.engine);
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, "views"));

Note: the documentation uses a period before the extension eg .hbs but it seems to work without this.注意:文档在扩展名之前使用了一个句点,例如.hbs但它似乎没有这个就可以工作。

Full code:完整代码:

var path = require('path');
var express = require('express');
var app = express();
var http = require('http').Server(app);

var handlebars = require('express-handlebars').create({
  layoutsDir: path.join(__dirname, "views/layouts"),
  partialsDir: path.join(__dirname, "views/partials"),
  defaultLayout: 'layout',
  extname: 'hbs'
});

app.engine('hbs', handlebars.engine);
app.set('view engine', 'hbs');
app.set('views', path.join(__dirname, "views"));

app.get('/', function(req, res){
  res.render('index');
});


http.listen(3000, function(){
  console.log("Server running");
});

In the new handlebars in your use partials you dont have to include its directory in server.js在您使用部分的新把手中,您不必在 server.js 中包含其目录

  • The partials are included in "express-handlebars"分音包含在“express-handlebars”中
  • The first line in view engine setup is responsible for the above视图引擎设置中的第一行负责以上内容

Take note of these.注意这些。

    const hbs = require('hbs');
    const expressHbs = require('express-handlebars');

//view engine setup
     app.engine('.hbs', expressHbs({ defaultLayout: 'layout', extname: '.hbs' }));
     app.set('view engine', 'hbs');
     app.set('views', path.join(__dirname, '../views'));
     app.use(express.static(path.join(__dirname, '../public')));

Try to use wildcard on your Dependencies to make sure you match the latest version尝试在您的依赖项上使用通配符以确保您匹配最新版本

"dependencies": {  
    "express": "*",
    "express-handlebars": "^3.0.0",
    "hbs": "*"
  },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM