我正在使用 express 和 ejs 建立一个网站:
"dependencies": {
"ejs": "^2.5.2",
"express": "^4.14.0",
在我的 app.js 中,我将 ejs 定义为模板引擎和 View 的根:
app.set('view engine', 'ejs'); // set view engine
app.set('views', 'app/views'); // set custom root for view engine
然后我创建了我的 index.ejs 文件,其中我包含了一个子目录的部分内容: 索引.ejs
<head><% include ./partials/template/head %></head>
文件夹结构:
- views
index.ejs
-- partials
-- -- template
head.ejs
当启动服务器时,索引加载时没有错误,但没有头部部分。 如果我更改包含(指向错误的位置),服务器无法开始突出显示问题,因此 ejs 能够找到 head.ejs。 如果我在 views 目录中移动 head.ejs,head 就会正确加载到 index.ejs 中。 所以……我有点纳闷,好像在子目录下文件读取了但没有载入include。
在四处搜索后,我尝试使用 express-partials,但没有太大帮助。
有什么线索吗?
干杯,乔瓦尼
最佳答案
像这样改变你的包含语句
<%- include("./partials/template/head.ejs") %>
这对我有用。
关于node.js - ejs include 不适用于子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40259512/