【问题标题】:node.js ejs include errornode.js ejs 包含错误
【发布时间】:2012-11-12 06:54:31
【问题描述】:

我正在创建这个模板:

<% include head %>
<Placemark>
    <name><%=name%></name>
    <description><%=description%></description>
    <Point>
        <coordinates><%=coordinates%></coordinates>
        </Point>
</Placemark>
<% include foot %>

但我总是得到这个错误:

if (!filename) throw new Error('filename option is required for includ
                         ^

目录:

justas@justas-Studio-1555:~/node-socket.io/socket.io/examples/kml$ ls -1
app.js
foot.ejs
head.ejs
placemark.ejs

有人可以帮忙吗,我根据工具箱应该一切正常

app.js:

var http = require('http');
var ejs = require('ejs');

var fs = require('fs')

http.createServer(function (req, res) {

res.writeHead(200, {'Content-Type': 'text/xml'});

fs.readFile('placemark.ejs', 'utf8', function (err, template) {

var content = ejs.render(template,{
    name:"test name",
    description:"this is the description",
    coordinates:"-122.0822035425683,37.42228990140251,0"
});

res.write(content);
res.end()
});
}).listen(8000);
console.log('Server listening at at xxxxxxxx');

我使用 ejs 渲染模板,该模板是从其他模板构造的。使用 ejs-locals 它说它没有方法渲染。有没有办法只用'ejs'来做到这一点??

【问题讨论】:

  • 你用的是什么版本的ejs和express?这看起来正确,是我使用的。 github.com/visionmedia/ejs
  • /home/justas ├── ejs@0.8.3 ├─┬ express@3.0.0

标签: node.js ejs


【解决方案1】:

刚刚碰到这个问题,下面是定义模板文件夹并将这些模板包含在主 ejs 文件中的方法:

var renderedHtml = ejs.render(content, 
                            {
                                data: data,
                                filename: __dirname + '/app/templates/*'
                            }
                        );

【讨论】:

    【解决方案2】:

    我最近也遇到了这个错误。我看到了 alexjamesbrown 的回答,但我不喜欢这个解决方案。我宁愿不为每个渲染都包含文件名变量;代码可能会变得混乱!我希望将文件包含在各个视图中。

    所以我挖掘了 ejs lib 文件并删除了对文件名变量的要求。

    您将在第 157 行的 \ejs\lib\ejs.js 中找到以下内容

    if (!filename) throw new Error('filename option is required for includes');
    

    只需注释掉该行并在您的 .ejs 文件中使用 &lt;% include views\include.ejs %&gt; 即可包含您的个人视图。

    以上对ejs版本0.8.4有效

    【讨论】:

    • 哇,你太棒了,太棒了。我刚刚在 github 上添加了一个错误。 github.com/visionmedia/ejs/issues/137
    • 干杯 @JasonS - 只是为了更新这不再在第 157 行。在 ejs\lib\ejs.js 文件中搜索此行。
    【解决方案3】:

    这是一个工作示例:

    看来您需要传入模板的文件名才能使用包含 - 请参阅example here

    var http = require('http');
    var ejs = require('ejs');
    
    var fs = require('fs');
    
    http.createServer(function (req, res) {
    
    res.writeHead(200, {'Content-Type': 'text/xml'});
    
    fs.readFile('placemark.ejs', 'utf8', function (err, template) {
    
    var content = ejs.render(template,{
        name:"test name",
        description:"this is the description",
        coordinates:"-122.0822035425683,37.42228990140251,0",
        filename: __dirname + '/placemark.ejs'
    });
    
    res.write(content);
    res.end()
    });
    }).listen(8000);
    console.log('Server listening at at xxxxxxxx');
    

    【讨论】:

    • 它无法识别“部分”'ReferenceError: ejs:1 >> 1| 2| 3| 4| 部分未定义'
    • 你在你的应用中使用 require('ejs') 吗?
    • partial 不受最新的 ejs 和 express 3 的支持。你需要 ejs-locals 来实现。
    • 使用 ejs-locals 它说它没有方法渲染。有没有办法只用'ejs'来做到这一点??
    • 感谢功能示例,它完成了工作:)
    猜你喜欢
    • 2017-11-03
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2011-07-21
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多