template模板的使用方法

模板


WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用。
定义模板

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

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

模板的作用域

模板拥有自己的作用域,只能使用data传入的数据。
example:
定义模板 
<template name="homecell">
     <view class="item">
            <image class="poster" src="{{item.images.small}}" />
            <view class="meta">
                <text class="title">{{item.title}}</text>
                <text class="sub-title">{{item.original_title}}({{item.year}})</text>
                <view class="artists">
                    <text wx:for="{{item.directors}}">{{item.name}}</text>
                </view>
            </view>
            <view class="rating">
                <text>{{item.rating.average}}</text>
            </view>
    </view>
 </template>
使用模板
<import src="../../commonXml/homecell.wxml" />  //引入模板
<loading hidden="{{!loading}}">加载中...</loading>
<scroll-view scroll-y="true" class="page-body">
    <navigator wx:for="{{movies}}" url="../movie/movie?id={{item.id}}">
       <template is="homecell" data="{{item}}"></template>  //声明需要的使用的模板使用data传入的数据。
    </navigator>
</scroll-view>
原文地址:https://www.cnblogs.com/tian-sun/p/7405837.html