【问题标题】:EJS Could not find include include fileEJS 找不到包含包含文件
【发布时间】: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,但如果你这么说,我会试一试
  • 没有括号给出同样的错误...
  • 我的意思是这样&lt;% include("../partials/bodyTemplate.ejs") %&gt;
  • 抛出错误; ^ 错误:ejs:13 11| 12|
    >> 13| 14|
    15| 16|
    找不到包含包含文件。

标签: javascript node.js express ejs


【解决方案1】:

问题是您在从fs 获取它作为字符串之后尝试编译html,这样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 = ejs.compile(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');

基本上,我传递{filename: path} 是为了告诉ejs 文件的原始位置。

虽然我相信你正在尝试做的事情,ejs 已经用renderFile 方法实现了它。 检查这个替代功能,它将做你想做的事。

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');

【讨论】:

  • 这很酷,但在这种情况下,我必须删除前面提到的括号。有括号没有显示bodyTemplate.html的内容
猜你喜欢
  • 1970-01-01
  • 2018-11-25
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-05
  • 2011-10-22
  • 2012-11-01
相关资源
最近更新 更多