2012-11-23 3 views
5

が含まれますツールボックスによると、すべてが動作するはずですのNode.jsの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」だけでこれを行う方法はありますか?ここで

+0

はどのバージョンejsと表現のあなたは使用していますか?これは正しいと私が使用するものです。 https://github.com/visionmedia/ejs – chovy

+0

/ホーム/ justas ├──[email protected] ├─┬[email protected]それは認識しない – sauletasmiestas

答えて

10

は実施例である:

あなたには、使用することができるようにするために、テンプレートのファイル名に渡す必要が表示されます - 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'); 
+0

"部分"「にReferenceError:EJS:1 > > 1 | <% - partial( 'head')%> 2 | 3 | <%=name%> 4 | ' – sauletasmiestas

+0

あなたのアプリでrequire(' ejs ')を使用していますか? – Alex

+1

部分は最新のejsでサポートされておらず、3を表現します。これにはejs-localsが必要です。 – chovy

3

を参照してください、私は最近、この出くわしましたエラーもあります。私はalexjamesbrownの答えを見ましたが、私は解決策が気に入らなかった。私はむしろすべてのレンダリングのファイル名変数を含めないでください。コードが乱雑になることがあります!個々のビューの中にファイルを含めることをお勧めします。

私はejsのlibファイルを掘り出し、filename変数の要件を削除しました。

は、あなたは、単にその行をコメントアウトライン157

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

に\ EJS \ libに\ ejs.jsに次のように検索して、個々のビューを含めるように.ejsファイルで<% include views\include.ejs %>を使用します。

上記EJSバージョン0.8.4

+0

うわー、これは素晴らしいです。私はちょうどgithubのこれに関するバグを追加しました。https://github.com/visionmedia/ejs/issues/137 – JasonS

+0

乾杯@JasonS - アップデートするだけで、これは157行目にはなくなりました。この行をejs \ lib \ ejs.jsファイルで検索してください。 –

0

はちょうどこの問題にぶつかったとここにあなたがテンプレートフォルダを定義することができる方法だと、あなたのメインのEJSファイルにこれらのテンプレートが含まれるため有効です

var renderedHtml = ejs.render(content, 
          { 
           data: data, 
           filename: __dirname + '/app/templates/*' 
          } 
         ); 
関連する問題