您的位置:首页 > 移动开发 > 微信开发

微信小程序-视图模板

2016-11-10 15:35 162 查看

定义模板

使用name属性,作为模板的名字。然后在
<template/>
内定义代码片段,如:

<!--
index: int
msg: string
time: string
-->
<template name="msgItem">
<view>
<text> {{index}}: {{msg}} </text>
<text> Time: {{time}} </text>
</view>
</template>


使用模板

使用 is 属性,声明需要的使用的模板,然后将模板所需要的 data 传入,如:

<template is="msgItem" data="{{...item}}"/>


Page({
data: {
item: {
index: 0,
msg: 'this is a template',
time: '2016-09-15'
}
}
})


is 属性可以使用 Mustache 语法,来动态决定具体需要渲染哪个模板:

<template name="odd">
<view> odd </view>
</template>
<template name="even">
<view> even </view>
</template>

<block wx:for="{{[1, 2, 3, 4, 5]}}">
<template is="{{item % 2 == 0 ? 'even' : 'odd'}}"/>
</block>


模板的作用域

模板拥有自己的作用域,只能使用data传入的数据。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: