简体   繁体   English

包含部分的 nodejs/ejs 语法错误

[英]nodejs/ejs syntax error with include partials

I have a really strange problem which I cannot figure out我有一个我无法弄清楚的非常奇怪的问题

i get this error when trying to load the webpage.尝试加载网页时出现此错误。 I am trying to very simply do views with partials for header and footer.我试图非常简单地使用页眉和页脚的部分视图。 I have tried every which way of doing it even starting a new project doing npm init, npm install express 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)

here is my main.js code located in the root这是位于根目录中的我的 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")}`);
});

here is my index.ejs located in /views这是我的 index.ejs 位于 /views

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

here is my layout.ejs also located in /views这是我的 layout.ejs 也位于 /views

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

my header and footer ejs files are located in /views/partials and are basically just divs for now with their respective ids.我的页眉和页脚 ejs 文件位于 /views/partials 中,现在基本上只是带有各自 ID 的 div。

here is my package.json located in the root which shows I have all the correct dependencies installed这是我的 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"
}
}

The error does not occur if I do not use the includes for header and footer in layout如果我不在布局中对页眉和页脚使用包含,则不会发生该错误

So either I did something wrong, or there is a problem with includes on ejs.所以要么我做错了什么,要么 ejs 上的包含有问题。

You have to put the partials in brackets and quotes.您必须将部分放在括号和引号中。 So you should write this part所以你应该写这部分

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

Like this像这样

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

so in case anyone else runs into this issue using ejs.所以万一其他人使用 ejs 遇到这个问题。 Apparently include is considered a function and thus requires ()显然 include 被认为是一个函数,因此需要 ()

so proper syntax would be所以正确的语法是

 <% include('somefile') %>

even though this is not specified in the documentation which calls it a keyword and not a function.即使这在文档中未指定,该文档将其称为关键字而不是函数。

Welp thats it, answered my own question thanks to @Saurabh就是这样,感谢@Saurabh 回答了我自己的问题

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

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