【问题标题】:jsRender looping but variables are emptyjsRender循环但变量为空
【发布时间】:2015-04-30 14:46:54
【问题描述】:

这是我的代码.. (snipped) (jsRender version v1.0.0-beta)

    <script type="text/javascript" src="resources/javascript/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="resources/javascript/jsrender.min.js"></script>

    <script>
    $(document).ready(function(){ 
        data.past = [ { text: 'HEllo' }, {text: 'bye'} , {text: 'bye'} ]
        var template = $.templates("#takeover");            
        output = template.render(data.past);
        $("table.events").append(output);
    }
    </script>

    <body>
    <script id="takeover" type="text/x-jsrender">
    <tr id="{{:takeover_id}}" class="dynamic">
        <td>{{:text}}{{if text}}yep{{else}}nope{{/if}}</td>
    </tr>
    </script>   
    <table class="events">
        <thead>
            <td>Takeover text</td>
        </thead>
    </table>
    </body>

因此,正如预期的那样,三个.dynamic tr 被插入到table.events

但是:而不是Hellobyebye;每个&lt;td&gt; 都简单地说yepnope

我看不出我做错了什么。

更新: 当我尝试console.log("{{test}}test") 这会在控制台中吐出test 时,是否有脚本干扰了双花括号?

【问题讨论】:

  • 发现了问题,我在节点中使用的路由器使用了一个模板系统,并且剥离了所有 {{ tags }}
  • 您可能有兴趣使用 JsRender 的功能来选择您自己的标签分隔符。见stackoverflow.com/questions/29493005/…

标签: jquery jsrender


【解决方案1】:
<script id="takeover" type="text/x-jsrender">
    <tr id="{{:takeover_id}}" class="dynamic">
        <td>{{:text}}{{if text}}yep{{else}}nope{{/if}}</td>
    </tr>
    </script> 
<table class="events">
        <thead>
            <td>Takeover text</td>
        </thead>
    </table>

JS:

var vm = [ { text: 'HEllo' }, {text: 'bye'} , {text: 'bye'} ];
var template = $.templates("#takeover");            
        output = template.render(vm);
        $("table.events").append(output); 

输出:

接管文本 埃洛耶普 再见 再见

【讨论】:

  • 和之前一样,三个&lt;td&gt;s 包含yesnope
  • 谢谢,你的代码是对的,我的问题是我在不知不觉中使用了两个冲突的模板引擎
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 2021-03-04
相关资源
最近更新 更多