node.js ejs包含错误 [英] node.js ejs include error
本文介绍了node.js ejs包含错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建此模板:
<% 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
有人可以帮忙吗,根据工具箱,我应该一切正常
Can someone help, I according to toolbox everything should work
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'??
using ejs I render template, which constructs from other templates. Using ejs-locals it says that it has no method render. Is there any way to do this with only 'ejs' ??
推荐答案
这是一个可行的示例:
似乎您需要传递模板的文件名,才能使用包含-请参见此处的示例
It appears you need to pass in the filename of the template, in order to be able to use include - See 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');
这篇关于node.js ejs包含错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文