node.js ejs包含错误

sau*_*tas 5 ejs node.js

我正在创建这个模板:

<% include head %>
<Placemark>
    <name><%=name%></name>
    <description><%=description%></description>
    <Point>
        <coordinates><%=coordinates%></coordinates>
        </Point>
</Placemark>
<% include foot %>
Run Code Online (Sandbox Code Playgroud)

但我总是得到这个错误:

if (!filename) throw new Error('filename option is required for includ
                         ^
Run Code Online (Sandbox Code Playgroud)

目录:

justas@justas-Studio-1555:~/node-socket.io/socket.io/examples/kml$ ls -1
app.js
foot.ejs
head.ejs
placemark.ejs
Run Code Online (Sandbox Code Playgroud)

可以有人帮忙,我根据工具箱一切都应该工作

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');
Run Code Online (Sandbox Code Playgroud)

使用ejs我渲染模板,从其他模板构造.使用ejs-locals它表示它没有方法渲染.有没有办法只用'ejs'来做到这一点?

Ale*_*lex 10

这是一个工作示例:

看来您需要传入模板的文件名,以便能够使用include - 请参阅此处的示例

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');
Run Code Online (Sandbox Code Playgroud)