nodejs / ejs语法错误,包含局部词

问题描述 投票:0回答:2

我有一个非常奇怪的问题,无法解决

我在尝试加载网页时收到此错误。我正在尝试非常简单地使用页眉和页脚的局部视图。我已经尝试了每种方法,甚至启动了一个新项目,执行npm init,npm install express ejs

SyntaxError: missing ) after argument list in layout.ejs while compiling ejs

If the above error is not helpful, you may want to try EJS-Lint:
https://github.com/RyanZim/EJS-Lint
Or, if you meant to create an async function, pass `async: true` as an option.
at new Function (<anonymous>)
at Template.compile (\node_modules\ejs\lib\ejs.js:626:12)
at Object.compile (\node_modules\ejs\lib\ejs.js:366:16)
at handleCache (\node_modules\ejs\lib\ejs.js:215:18)
at tryHandleCache (\node_modules\ejs\lib\ejs.js:254:16)
at View.exports.renderFile [as engine] (\node_modules\ejs\lib\ejs.js:459:10)
at View.render (\node_modules\express\lib\view.js:135:8)
at tryRender (\node_modules\express\lib\application.js:640:10)
at Function.render (\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (\temp\node_modules\express\lib\response.js:1012:7)

这是我的main.js代码位于根目录中

express = require("express");
layouts = require("express-ejs-layouts");
app = express();
app.set("port",process.env.PORT || 3000);
app.set("view engine", "ejs");
const homeController = require("./controllers/homeController");
app.use(layouts);
app.get("/users/:userName", homeController.respondWithuName);
app.listen(app.get("port"), () => {
    console.log(`Server is running on blah blah port: ${app.get("port")}`);
});

这是我的index.ejs位于/ views

<h1> Hello, <%= uname %></h1>

这也是我的layout.ejs也位于/ views

<body>
<%- include partials/header.ejs %>
<%- body %>
<%- include partials/footer.ejs %>
</body>

我的页眉和页脚ejs文件位于/ views / partials中,目前基本上只是带有其各自ID的div。

这是我的package.json位于根目录中,显示我已经安装了所有正确的依赖项

 {
"name": "test",
"version": "1.0.0",
"description": "test",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "test"
},
"author": "test",
"license": "ISC",
"dependencies": {
"ejs": "^3.0.1",
"express": "^4.17.1",
"express-ejs-layouts": "^2.5.0"
}
}

如果我不在布局中使用页眉和页脚的包含项,则不会发生此错误

所以我做错了,或者ejs中的include出现了问题。

javascript node.js express ejs node-modules
2个回答
0
投票

以防其他人使用ejs遇到此问题。显然include被认为是一个函数,因此需要()

因此将使用适当的语法

 <% include('somefile') %>

即使在文档中未指定此名称,该文档将其称为关键字而不是函数。

就这样,感谢@Saurabh回答了我自己的问题


0
投票

您必须将分项放在方括号和引号中。所以你应该写这部分

<body>
<%- include partials/header.ejs %>
<%- body %>
<%- include partials/footer.ejs %>
</body>

喜欢这个

<body>
<%- include ("partials/header") %>
<%- body %>
<%- include ("partials/footer") %>
</body>
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.