JsRender for object syntax introduction, jsrenderobject

Source: Internet
Author: User

JsRender for object syntax introduction, jsrenderobject

This article briefly describes the basic syntax of JsRender for object. Share it with you for your reference. The details are as follows:
 
As a JavaScript template engine, JsRender has a loop function, that is, for. However, because JsRender is too flexible, for can accept objects as cyclic objects.

Both {for Array} and {for Object} are allowed. {for Array} can be understood as traversing an Array to retrieve each element one by one. However, {for Object} is a bit confusing, and the official document only provides an example of no help.

At the beginning, I thought that the purpose of {for Object} was to traverse all the attributes of the Object. But after careful consideration, this function {props Object} has been implemented, the props tag is used to traverse all the attributes of the Object. The number of attributes indicates the number of cycles. Each loop has two hidden attributes: key, prop, and key, which represent the attribute name, prop indicates the property value, which is very convenient to use.

In fact, {for Object} is not a loop. It can be understood as "Into". That is to say, it enters the Object environment and sets the current context as an Object, similar to Handlebars. with in js.

For example:

Data:

Copy codeThe Code is as follows :{
"Title": "The A team ",
"Members ":[
{
"Name": "Pete ",
"City": "members_city ",
"Address ":{
"City": "address_city ",
"City1": "address_city1 ",
"City2": "address_city2"
}
}
]
}

Template markup:

Copy codeCode: {for members }}
{For address }}
<P>. {{: city }}</p>
{/}}
{/}}

Result:

Copy codeThe Code is as follows: address_city
 

It can be seen from the results that although members's item also has the city attribute, but because {for address} is passed into the Object pointed to by address ,{{: city} is obtained directly from the address.

At the same time, the address has three attributes, but only one row is output. It turns out that {for Object} does not loop, just switching this.

I hope this article will help you learn about JsRender programming.




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.