EJS找不到包含文件
这可能听起来像this和其他很少的重复,但这些堆栈中给出的解决方案没有帮助。
我正在使用EJS模板创建HTML,但包含部分我收到以下错误:
throw new Error('Could not find include include file.');
以下是项目结构
|
|
|-----index.js
|-----view
| |
| |----pages
| | |----mainTemplate.ejs
| |----partials
| | |----bodyTemplate.ejs
| |
现在按照doc,include采用相对路径,因此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>
虽然bodyTemlate.ejs中的代码是
<% console.log("#########################");%>
<h1>Hi there !! </h1>
我收到无法找到包含文件这个错误没有太多具体到这里访问的路径。
我试过删除..从路径仍然没有用?我的EJS应用程序中有相同的项目结构,这个相同的代码片段工作正常吗?这是否具体表达导致此类问题?
编辑
这是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 = ejspile(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');
回答如下:
问题是你试图编译html后,你从fs
得到一个字符串,这样ejs
不知道该文件位于何处以及在哪里寻找相对模板。
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 = ejspile(ejs_string, {
filename: path
}),
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');
EJS找不到包含文件
这可能听起来像this和其他很少的重复,但这些堆栈中给出的解决方案没有帮助。
我正在使用EJS模板创建HTML,但包含部分我收到以下错误:
throw new Error('Could not find include include file.');
以下是项目结构
|
|
|-----index.js
|-----view
| |
| |----pages
| | |----mainTemplate.ejs
| |----partials
| | |----bodyTemplate.ejs
| |
现在按照doc,include采用相对路径,因此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>
虽然bodyTemlate.ejs中的代码是
<% console.log("#########################");%>
<h1>Hi there !! </h1>
我收到无法找到包含文件这个错误没有太多具体到这里访问的路径。
我试过删除..从路径仍然没有用?我的EJS应用程序中有相同的项目结构,这个相同的代码片段工作正常吗?这是否具体表达导致此类问题?
编辑
这是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 = ejspile(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');
回答如下:
问题是你试图编译html后,你从fs
得到一个字符串,这样ejs
不知道该文件位于何处以及在哪里寻找相对模板。
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 = ejspile(ejs_string, {
filename: path
}),
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');