Jsrender for array and for object syntax

Source: Internet
Author: User

For Array Loop array

Recycling Use Cases

Defining array Data

var  data = {   names: ["Maradona", "Pele", "Ronaldo", "Messi"]}

Used in templates

{{for names}}   < Div >    < b > {{: #index +1}}. </ b >    < span > {{: #data}} </ span >  </ Div > {{/for}}

Output results

1. Maradona2. Pele3. Ronaldo4. Messi

An index can be obtained by a special literal #index in a loop, with a special literal #data equivalent to this, which in this case represents each name.

When you use the For loop in another jsrender, you do not create regular data scopes, but interfere with hidden scopes, such as when you use IF. That is, {{if}} does not block the visibility of regular data (your incoming data), but it interferes with the visibility of hidden data (#index, #parent).

So try to get the index using the #getIndex () when getting the index subscript , avoid using #indexunless your app is simple enough.

You can try the difference between the following two uses

1.

{{for names}}  {{if #data. IndexOf ("M") = = 0}    } < Div >      < b > {{: #index +1}}. </ b >      < span > {{: #data}} </ span >    </ Div >   {{/if}}{{/for}}

2.

{{for names}}   {{if #data. IndexOf ("M") = = 0}     } < Div >       < b > {{: #getIndex () +1}}. </ b >       < span > {{: #data}} </ span >     </ Div >    {{/if}} {{/for}}

The For object enters object

Here for does not represent the loop, which represents the entry into the object. Sets the current context to object, similar to with in Handlebars.js.

As an example:

var data ={  "title": "The A team",  "members": [    {      "name": "Pete",      "City": "Members_city",      " Address ": {        " city ":" Address_city ",        " city1 ":" Address_city1 ",        " City2 ":" Address_city2 "      }    }  ]}

Templates

{{for members}}  {{for address}}     < P >. {: City}} </ P >   {{/for}}{{/for}}

Results

Address_city

As you can see from the results, although there is a city attribute under the members item, the {{: city}} was directly fetched from address because it entered the object pointed to in address through {{for address}}.

At the same time, the address has three attributes, but the result only outputs a single line, proving that {{for Object}} is not looping, just switching this.

Jsrender for array and for object syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.