微信小程序template的使用
在小程序开发的过程中,很多时候有些模板是一样的,那么我们就可以使用template来实现共用一个模板块。具体实现如下:
一、定义模板
1、新建一个template文件夹用来管理项目中所有的模板;
2、新建一个common.wxml文件来定义模板;
3、使用name属性,作为模板的名字。然后在<template/>内定义代码片段。
<template name="courseLeft">
<navigator url="../play/play?courseUuid={{courseUuid}}&isCompany={{isCompany}}">
<view class="item mr26">
<image src="{{imagePath}}" mode="aspectFill"></image>
<view class="course-title">
<text class="title">{{courseName}}</text>
<text class="author">- {{teacherName}}</text>
</view>
<view class="course-info clearfix">
<view class="fl"><text class="play">{{playCount}}</text></view>
<view class="fr"><text class="grade">{{score}}</text></view>
</view>
<view wx:if="{{studyProgress}}" class="tip-completed">{{studyProgress}}</view>
</view>
</navigator>
</template>
<template name="courseRight">
<navigator url="../play/play?courseUuid={{courseUuid}}&isCompany={{isCompany}}">
<view class="item">
<image src="{{imagePath}}" mode="aspectFill"></image>
<view class="course-title">
<text class="title">{{courseName}}</text>
<text class="author">- {{teacherName}}</text>
</view>
<view class="course-info clearfix">
<text class="play fl">{{playCount}}</text>
<text class="grade fr">{{score}}</text>
</view>
<view wx:if="{{studyProgress}}" class="tip-completed">{{studyProgress}}</view>
</view>
</navigator>
</template>
二、使用模板
1、使用 is 属性,声明需要的使用的模板
1、<import src="../../templates/common.wxml"/>
2、将模板所需要的 data 传入,一般我们都会使用列表渲染。
<block wx:for="{{courseList}}">
<template is="{{index%2 === 0 ? 'courseLeft' : 'courseRight'}}" data="{{...item}}"></template>
</block>
注意:
a.可以通过表达式来确定使用哪个模板is="{{index%2 === 0 ? 'courseLeft' : 'courseRight'}}"
或者通过wx:if来确定。index是数组当前项的下标。
1.<template wx:if="{{index%2 === 0}}" is="courseLeft" data="{{...item}}"></template>
2.<template wx:else is="courseRight" data="{{...item}}"></template>
data 是要模板渲染的数据,data="{{...item}}" 写法是ES6的写法,item是wx:for当前项,... 是展开运算符,在模板中不需要再{{item.courseName}} 而是直接{{courseName}} 。
更多技术问题,可前往:http://sucai.gxyourui.cn/Home/Article/blog
阅读更多- 微信小程序如何使用template
- 微信小程序-template模板使用
- 微信小程序中template模板使用
- [置顶] 微信小程序使用template标签实现五星评分
- [转]微信小程序-template模板使用
- 微信小程序 模板template的使用
- 微信小程序 template模板的使用
- 转发:微信小程序-template模板使用
- 微信小程序使用template标签实现五星评分功能
- 微信开发之微信小程序template使用
- 微信小程序模板(template)使用详解
- 快速了解微信小程序的使用,一个根据小程序的框架开发的 todos app
- 微信小程序 (应用号)开发(教程)文档,内附开发工具及工具使用方法
- 微信小程序教程——使用checkbox显示多项选择框
- 微信小程序例子——使用progress组件实现读取进度效果
- 微信小程序例子——使用radio显示单选项
- 微信小程序例子——使用input组件实现密码框
- 微信小程序例子——如何使用scroll-view组件实现视图垂直滚动
- 微信小程序例子——使用icon组件显示常用图标