Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ICanHaz.js - Possible to put a while loop in template?

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>
    
  • like image 416
    Danny Anges Avatar asked Dec 21 '22 10:12

    Danny Anges


    1 Answers

    icanhaz (moustache) does include a way to loop.

    In javascript:

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

    In your view:

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

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

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

    like image 59
    bostonou Avatar answered Jan 07 '23 19:01

    bostonou