微信小程序 教程之模板
系列文章:
微信小程序教程之WXSS
微信小程序教程之引用
微信小程序教程之事件
微信小程序教程之模板
微信小程序教程之列表渲染
微信小程序教程之条件渲染
微信小程序教程之数据绑定
微信小程序教程之WXML
模板
WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用。
定义模板
使用name属性,作为模板的名字。然后在<template/>内定义代码片段,如:
<!--
index:int
msg:string
time:string
-->
<templatename="msgItem">
<view>
<text>{{index}}:{{msg}}</text>
<text>Time:{{time}}</text>
</view>
</template>
使用模板
使用is属性,声明需要的使用的模板,然后将模板所需要的data传入,如:
<templateis="msgItem"data="{{...item}}"/>
Page({
data:{
item:{
index:0,
msg:'thisisatemplate',
time:'2016-09-15'
}
}
})
is属性可以使用Mustache语法,在运行时来决定具体需要渲染哪个模板:
<templatename="odd">
<view>odd</view>
</template>
<templatename="even">
<view>even</view>
</template>
<blockwx:for="{{[1,2,3,4,5]}}">
<templateis="{{item%2==0?'even':'odd'}}"/>
</block>
模板的作用域
模板拥有自己的作用域,只能使用data传入的数据。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!