5

私はこのテンプレートを作成しています:

<% 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を使用すると、renderメソッドがないことがわかります。'ejs'だけでこれを行う方法はありますか?

4

3 に答える 3

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');
于 2012-11-23T23:59:30.680 に答える
3

私も最近このエラーに遭遇しました。私はalexjamesbrownの答えを見ましたが、解決策が気に入らなかった。レンダリングごとにfilename変数を含めたくはありません。コードが乱雑になる可能性があります!個々のビュー内にファイルを含めたいと思います。

そこで、ejs libファイルを掘り下げて、filename変数の要件を削除しました。

157行目の\ejs\ lib\ejs.jsに次の情報があります。

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

その行をコメントアウトし<% include views\include.ejs %>、.ejsファイルで使用して個々のビューを含めるだけです。

上記はejsバージョン0.8.4に有効です

于 2013-06-09T13:41:19.633 に答える
0

この質問にぶつかっただけで、テンプレートフォルダーを定義し、それらのテンプレートをメインのejsファイルに含める方法は次のとおりです。

var renderedHtml = ejs.render(content, 
                            {
                                data: data,
                                filename: __dirname + '/app/templates/*'
                            }
                        );
于 2017-07-24T08:16:01.380 に答える