代码之家  ›  专栏  ›  技术社区  ›  Genaut

jsrender/jsview在我使用for循环时重复所有模板

  •  0
  • Genaut  · 技术社区  · 6 年前

    我有一个数组,格式如下:

    {[
    { title : "", artist : "" },
    { title : "", artist : "" },
    { title : "", artist : "" },
    { title : "", artist : "" }
    ]}
    

    当我尝试打印数据时,我得到完整的模板n次(Json数据长度)我有:

    var list = new Array();
    list.push({ title : **data**, artist : **data** });
    var songsTemplate = $.templates("#topSongsTemplate");
    var html = songsTemplate.render(list);
    $("#topSongs").html(html);
    

    我只想为我的模板重复。但是我在我的部门里总是重复得到完整的模板。我遗漏了什么?以下内容:

    <script type="text/x-jsrender" id="topSongsTemplate">
        <div class="col-md-6">
            <i class="fa fa-trophy"></i> <span data-i18n="search.top_tracks">Top 20 Tracks</span>
            <div class="separator hr-line-dashed m-t-xs"></div>
            <ol>
                {{for ~root }}
                    <li>{{>title}} - <small>{{>artist}}</small></li>
                {{/for}}
            </ol>
        </div>
        </script>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   BorisMoore    6 年前

    要在不进行迭代的情况下渲染,可以编写:

    var html = songsTemplate.render(list, true);
    

    将数组传递给render(),但不进行迭代。 在里面 this documentation topic 是的。

    顺便说一句,你可以同样地写 {{for}} {{for #data}} -尽管 {{for ~root}} 也会起作用的。

    也看看 this answer 类似的问题。