admin管理员组文章数量:1292314
This might sound like duplicate of this and few others but the solutions given in those stack were not helpful.
I am creating an HTML using EJS templating, but while including a partial I am receiving following error :
throw new Error('Could not find include include file.');
Below is the project structure
|
|
|-----index.js
|-----view
| |
| |----pages
| | |----mainTemplate.ejs
| |----partials
| | |----bodyTemplate.ejs
| |
Now as per doc the include takes relative path so code in my mainTemplate.ejs includes the bodyTemplate like this
<!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>
While code in bodyTemlate.ejs is
<% console.log("#########################");%>
<h1>Hi there !! </h1>
I am receiving Could not find include include file This error is not much specific as to which path is being accessed here.
I have tried removing .. from the path still no use? I have same project structure in my EJS application and this same code snippet works just fine? Is this something specific to express causing such issue?
EDIT
This is 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');
This might sound like duplicate of this and few others but the solutions given in those stack were not helpful.
I am creating an HTML using EJS templating, but while including a partial I am receiving following error :
throw new Error('Could not find include include file.');
Below is the project structure
|
|
|-----index.js
|-----view
| |
| |----pages
| | |----mainTemplate.ejs
| |----partials
| | |----bodyTemplate.ejs
| |
Now as per doc the include takes relative path so code in my mainTemplate.ejs includes the bodyTemplate like this
<!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>
While code in bodyTemlate.ejs is
<% console.log("#########################");%>
<h1>Hi there !! </h1>
I am receiving Could not find include include file This error is not much specific as to which path is being accessed here.
I have tried removing .. from the path still no use? I have same project structure in my EJS application and this same code snippet works just fine? Is this something specific to express causing such issue?
EDIT
This is 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.pile(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');
Share
edited Dec 22, 2017 at 7:00
TGW
asked Dec 22, 2017 at 6:25
TGWTGW
83511 silver badges29 bronze badges
8
- did you forgot the parenthesis in include? – Alex Michailidis Commented Dec 22, 2017 at 6:30
- well I've a working code snippet without parenthesis, but will give it a shot if you say so – TGW Commented Dec 22, 2017 at 6:34
- no parenthesis gives same error... – TGW Commented Dec 22, 2017 at 6:35
-
i mean like this
<% include("../partials/bodyTemplate.ejs") %>
– Alex Michailidis Commented Dec 22, 2017 at 6:37 - throw err; ^ Error: ejs:13 11| 12| <div class="starter-template"> >> 13| <% include("../partials/bodyTemplate.ejs") %> 14| </div> 15| 16| </div> Could not find include include file. – TGW Commented Dec 22, 2017 at 6:39
1 Answer
Reset to default 11The problem was that you are trying to pile the html after you got it as a string from fs
that way ejs
has no idea where that file located and where to look for relative templates.
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.pile(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');
Basically, I pass {filename: path}
in order to tell ejs
where the file is originally located.
Although I believe the thing you are trying to do, ejs
has already implemented it with the renderFile
method.
Check this alternate function that will do what you want.
var fs = require('fs'),
ejs = require("ejs");
function ejs2html2(path, information) {
ejs.renderFile(path, function (err, html) {
if (err) {
console.log("ERROR: " + err);
return false;
}
fs.writeFile(path + '.html', html, function (err) {
if (err) {
console.log(err);
return false
}
return true;
});
})
}
ejs2html2(__dirname + '/view/pages/mainTemplate.ejs');
本文标签: javascriptEJS Could not find include include fileStack Overflow
版权声明:本文标题:javascript - EJS Could not find include include file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741553187a2384997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论