JsRender for object 语法说明
for 说明 语法 object
2023-09-27 14:22:22 时间
JsRender 作为一款JavaScript模版引擎,必不可少的会有循环功能,也就是for,但由于JsRender过于灵活,for竟然可以接受object作为循环对象。
{{for Array}}和{{for Object}}都是允许的,{{for Array}}大家都能理解,就是遍历一个数组,逐个取出每个元素。但{{for Object}}就有点让人匪夷所思了,而且官方文档只是举了个没什么帮助的例子,其他无任何说明。
刚开始小菜以为{{for Object}}的用意是遍历该Object的所有属性,但仔细一想,这个功能{{props Object}}已经实现了,props标签的作用就是遍历Object所有属性,有多少个属性,就循环多少次,每次循环都会有两个隐藏的属性:key,prop,key代表属性名,prop代表属性值,用起来非常方便。
实际上,{{for Object}}并不是循环,它可以理解成进入(Into),也就是说进入到Object环境中,把当前的上下文设置为Object,类似于Handlebars.js中的with。
举个例子:
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", 10 "city2": "address_city2" 11 } 12 } 13 ] 14 }
template markup:
1 {{for members}} 2 {{for address}} 3 <p>.{{:city}}</p> 4 {{/for}} 5 {{/for}}
result:
1 address_city
从结果可以看出,虽然members的item下也有city属性,但由于通过{{for address}}进入到了address指向的Object中,{{:city}}直接从address中获取了。
同时,address有三个属性,但结果只输出了一行,证明{{for Object}}是不会循环的,仅仅是切换一下this。
相关文章
- Nuget connection attempt failed "Unable to load the service index for source"
- et kafka data error:UnsupportedCodecError: Libraries for lz4 compression codec not found
- 解决sublime package control 出现There are no packages available for installation
- Row Cache For InnoDB 第二版优化(附带安装和配置说明)
- java数组的拷贝四种方法:for、clone、System.arraycopy、arrays.copyof
- No toolchains found in the NDK toolchains folder for ABI with prefix: arm-linux-androideabi
- Linux-004-解决 Tomcat 启动时提示 Insufficient space for shared memory file
- arcgis api 4.x for js之图层管理篇
- 如何加入该网站for Linux(绑定域名)
- SVN Error:Error performing cleanup for
- visual studio 2019安装entity framework for mysql entity framework报错 由于以下异常而无法生成模型:’System.Data.StrongTypingException:表’TableDetails’中列’IsPrimaryKey’的值为DBNull
- How to configure Gzip for JBoss?---refer
- 《R3Det:Refined Single-Stage Detector with Feature Refinement for Rotating Object》论文笔记
- 《RVOS:End-to-End Recurrent Network for Video Object Segmentation》论文笔记
- 《LEDNet:A Lightweight Encoder-Decoder Network For Real-Time Semantic Segmentation》论文笔记