JsRender for object syntax
- 2020-03-30 04:12:13
- OfStack
This article briefly describes the basic syntax of JsRender for object. Share with you for your reference. The details are as follows:
JsRender as a JavaScript template engine, it is essential to have a loop function, that is, for, but because JsRender is too flexible, for can accept object as a loop object.
{{for Array}} and {{for Object}} are both allowed. But {{for Object}} is a bit of a mystery, and the official documentation just gives an unhelpful example, nothing more.
For the Object with a first side think {{}} was designed to traverse the all attributes of the Object, but a careful thought, the function {{props Object}} has achieved, the role of props tag iterates through all attributes of the Object is, how many properties, how many times will cycle, each cycle has two hidden attributes: key, prop, key on behalf of the property name, prop on behalf of attribute values, which are very convenient.
In fact, {{for Object}} is not a loop, it can be understood as Into, that is, Into the Object environment, set the current context to Object, similar to handlepuns.js with.
For example:
Data:
{
"title": "The A team",
"members": [
{
"name": "Pete",
"city": "members_city",
"address": {
"city": "address_city",
"city1": "address_city1",
"city2": "address_city2"
}
}
]
}
The template markup:
{{for members}}
{{for address}}
<p>.{{:city}}</p>
{{/for}}
{{/for}}
The result:
address_city
As can be seen from the results, although the item of members also has the city attribute, since it entered the Object pointed to by address through {{for address}}, {{:city}} was directly obtained from the address.
At the same time, the address has three attributes, but the result only outputs one line, proving that {{for Object}} does not loop, just toggle this.