下划线js模板中的引用错误

发布于 2025-01-03 21:18:37 字数 1931 浏览 2 评论 0原文

我正在使用带有节点js的backbonejs尝试将变量从主干视图传递到模板(html文件)。渲染视图模板的函数是这样的:

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)

如何解决这个问题???

i am using backbonejs with node js trying to pass variable from backbone view to template(html file). function for rendering view template is like this :

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

myPhoto having a value :

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

and in html file :

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

when i am execute it gives an error like

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)

how to solve this???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少心凉 2025-01-10 21:18:37

您确定您的所有照片都有标题吗?空属性可能会导致此错误。您可以通过在主干模型类中添加默认值(例如空字符串)来解决此问题。

Are you sure all your photos have a title? Empty properties can cause this error. You can fix this by adding default values (empty strings for example) in your backbone Model class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文