定义&使用

在home下新建模板文件夹templates,继续在templates里面新建temp.wxml:

  1. <!-- 定义模板 用name属性起名字 -->
  2. <template name="tmpl1">
  3. <view>这是模板1</view>
  4. </template>
  5. <template name="tmpl2">
  6. <view>这是模板2</view>
  7. </template>
<!-- 1 通过import标签进行引入 -->
<import src="./templates/temp.wxml"></import>
<!-- 2 通过is属性指定模板 -->
<template is="tmpl1"></template>
<template is="tmpl2"></template>

传值:

home.wxml中

<import src="./templates/temp.wxml"></import>
<!-- 通过data属性向模板内部传入数据 -->
<template is="tmpl1" data="{{msg:'呵呵'}}"></template>
<template is="tmpl2" data="{{aaa:'哈哈'}}"></template>

temp.wxml中:

<template name="tmpl1">
  <view>这是模板1:{{msg}}</view>
</template>
<template name="tmpl2">
  <view>这是模板2:{{aaa}}</view>
</template>