JsRender for Object Syntax description

Source: Internet
Author: User

JsRender as a JavaScript template engine, it is necessary to have a loop function, that is, for, but because JsRender is too flexible, for unexpectedly can accept object as a looping object.

{{for array}} and {{for Object}} are all allowed, {{for array}} Everyone can understand that it is to iterate through an array and remove each element one at a. But {{for Object}} is a bit of a freak, and the official documentation is just an example of nothing to help, no other explanation.

At first, it was thought that {{for Object}} was intended to traverse all the properties of the object, but carefully thought that the function {{props Object}} had already been implemented, and the function of the props tag was to iterate through all the properties of the object, how many properties were there, the number of loops, Each cycle has two hidden properties: Key,prop,key represents the property name, and prop represents the property value, which is very convenient to use.

In fact, {{for Object}} is not a loop, it can be understood as entering (into), which means entering into the object environment, setting the current context to object, similar to with in Handlebars.js.

As an example:

Data

1 {2"title": "The A team",3"Members": [4     {5"Name": "Pete",6"City": "Members_city",7"Address": {8"City": "Address_city",9"City1": "Address_city1",Ten"City2": "Address_city2" One       } A     } -   ] -}

Template markup:

1 {{ for "} }2   {{ for Address}}3     <p . {{:city}}</p>4   {{/for}}5 {{/for}}

Result

1 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 Object Syntax description

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.