ICanHaz.js-可以在模板中放置while循环吗? [英] ICanHaz.js - Possible to put a while loop in template?

查看:112
本文介绍了ICanHaz.js-可以在模板中放置while循环吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个元素,并且要在其中放置无限数量的

  • 项目(基于用户的选择).有没有一种方法可以创建允许某种while循环的ICanHaz模板.例如:

    Let's say I have a element, and inside it want to put an indefinite number of

  • items (based on the user's choices). Is there a way to create an ICanHaz template that allows for some sort of while loop. For instance:

        <ul>
         for(i = 0; i < numOfLi; i++)
           <li> {{ stuff }} </li>
        </ul>
    

    推荐答案

    icanhaz(小胡子)确实包含一种循环方法.

    icanhaz (moustache) does include a way to loop.

    在javascript中

    In javascript:

    var listOfStuff = {stuff: [ 
                          {key: "1", desc: "First"},
                          {key: "2", desc: "Second"}
                      ]};
    $("#mySelectBox").append(ich.myTemplate(listOfStuff));
    

    您认为:

    <script id="myTemplate" type="text/html">
      {{#stuff}}
        <option value="{{key}}">{{desc}}</option>
      {{/stuff}}
    </script>
    
    <select id="mySelectBox">
    </select>
    

    {{#stuff}}{{/stuff}}分隔列表.有关详细信息,请参见胡子 Sections 部分.

    The {{#stuff}} and {{/stuff}} delimit the list. Look at the Sections part of moustache for details.

    确保检查出此答案如果您使用的是jQuery 1.9或更高版本.

    Make sure to check out this answer if you're using jQuery 1.9 or above.

    这篇关于ICanHaz.js-可以在模板中放置while循环吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆