2012-02-08 11 views
0

私はノードjsでバックボーンビューからテンプレート(htmlファイル)に変数を渡そうとしているbackbonejsを使用しています。ビューテンプレートをレンダリングするための機能が、このようなものです:アンダースコアのテンプレートjs​​テンプレート

render: function(event){ 
     var compiled_template = _.template($("#results-template").html()); 
     console.log(myPhoto.toJSON()); 
     $(this.el).html(compiled_template(myPhoto.toJSON()));  
     return this; 
    } 

myPhoto持つ値:

{ 
     src: 'placeholder.jpg', 
     title: 'an image placeholder', 
     coordinates: [0,0], 
     tags: ['untagged'], 
     location: 'home' 
} 

とhtmlファイルに:私は実行していたときに

<script id="results-template" type="text/template">    
     <h2><%= title %></h2> 
     <p>testing....</p> 
    </script> 

はそれが

のようなエラーになります
ReferenceError: c:\trello\testApp/views/test.html:19 
    17|  <script id="results-template" type="text/template">  
    18|   
>> 19|   <h2><%= title %></h2>   
    20|   <p>testing....</p> 
    21|  </script> 
    22|  

title is not defined 
    at Object.<anonymous> (eval at <anonymous> (c:\trello\node_modules\ejs\lib\ejs.js:203:1)) 
    at Object.<anonymous> (c:\trello\node_modules\ejs\lib\ejs.js:201:15) 
    at ServerResponse._render (c:\trello\node_modules\express\lib\view.js:425:21) 
    at ServerResponse.render (c:\trello\node_modules\express\lib\view.js:318:17) 
    at c:\trello\testApp\test.js:23:16 
    at callbacks (c:\trello\node_modules\express\lib\router\index.js:272:11) 
    at param (c:\trello\node_modules\express\lib\router\index.js:246:11) 
    at pass (c:\trello\node_modules\express\lib\router\index.js:253:5) 
    at Router._dispatch (c:\trello\node_modules\express\lib\router\index.js:280:4) 
    at Object.handle (c:\trello\node_modules\express\lib\router\index.js:45:10) 

これを解決する方法???

+0

はい、動作していますが、node.jsを使用してexcuteするとエラーになります。 –

+0

私は、サーバー側でnode.js/ejsを使用し、クライアント側でbackbone.jsを使用します。サーバーサイドとクライアントサイドの両方で、同じテンプレートスタイルが使用されます。だから問題は、クライアントのためのテンプレートコードをテンプレート内に置くと、getは解析されないということです。 –

答えて

0

すべての写真にタイトルが付いていますか?空のプロパティが原因でこのエラーが発生する可能性があります。これを修正するには、バックボーンModelクラスにデフォルト値(空の文字列など)を追加します。

関連する問題