nodejs / ejs语法错误,包含部分内容

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

尝试加载网页时出现此错误。我正在尝试非常简单地使用页眉和页脚的局部视图。我尝试了每种方式,甚至启动了一个新项目,执行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出现了问题。

xiyangfenghuang 回答:nodejs / ejs语法错误,包含部分内容

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

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

    <body>
    <%- include ("partials/header") %>
    <%- body %>
    <%- include ("partials/footer") %>
    </body>
,

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

所以语法正确

 <% include('somefile') %>

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

很高兴,感谢@Saurabh

回答了我自己的问题
本文链接:https://www.f2er.com/2953173.html

大家都在问