【发布时间】:2018-06-04 19:54:43
【问题描述】:
这听起来像是 this 和其他少数人的重复,但这些堆栈中给出的解决方案没有帮助。
我正在使用 EJS 模板创建 HTML,但在包含部分内容时收到以下错误:
throw new Error('Could not find include include file.');
下面是项目结构
|
|
|-----index.js
|-----view
| |
| |----pages
| | |----mainTemplate.ejs
| |----partials
| | |----bodyTemplate.ejs
| |
现在根据文档,包含采用相对路径,因此我的 mainTemplate.ejs 中的代码包含这样的 bodyTemplate
<!DOCTYPE html>
<html lang="en">
<head>
<title>Sample</title>
</head>
<body>
<!-- Insert Body here -->
<div class="container">
<div class="starter-template">
<% include ../partials/bodyTemplate.ejs%>
</div>
</div>
</body>
</html>
bodyTemplate.ejs 中的代码是
<% console.log("#########################");%>
<h1>Hi there !! </h1>
我收到找不到包含包含文件 此错误与此处访问的路径不太具体。
我已尝试从路径中删除 .. 仍然没有用?我在我的 EJS 应用程序中具有相同的项目结构,并且相同的代码 sn-p 可以正常工作吗? 这是特定于表达导致此类问题的东西吗?
编辑
这是 index.js
var fs = require('fs'),
ejs = require("ejs");
function ejs2html(path, information) {
fs.readFile(path, 'utf8', function(err, data) {
if (err) {
console.log("ERROR: " + err);
return false;
}
var ejs_string = data,
template = ejs.compile(ejs_string),
html = template(information);
fs.writeFile(path + '.html', html, function(err) {
if (err) {
console.log(err);
return false
}
return true;
});
});
}
// console.log("This is path --> ", __dirname + '/view/pages/bodyTemplate.ejs');
ejs2html(__dirname + '/view/pages/mainTemplate.ejs');
【问题讨论】:
-
你忘记了include中的括号吗?
-
好吧,我有一个不带括号的工作代码 sn-p,但如果你这么说,我会试一试
-
没有括号给出同样的错误...
-
我的意思是这样
<% include("../partials/bodyTemplate.ejs") %> -
抛出错误; ^ 错误:ejs:13 11| 12|找不到包含包含文件。>> 13| 14|15| 16|
标签: javascript node.js express ejs