jsRender循环但变量为空

时间:2015-04-30 14:46:54

标签: jquery jsrender

这是我的代码..(剪掉)(jsRender版本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;每个<td>只说yepnope

我看不出我做错了什么。

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

1 个答案:

答案 0 :(得分:0)

<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); 

输出:

接管文字 HElloyep byeyep byeyep