PHP前端开发

微信小程序 教程之模板

百变鹏仔 5小时前 #前端问答
文章标签 模板

模板

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

定义模板

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

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

使用模板

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

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

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

<template>   <view> odd </view>  </template>  <template>   <view> even </view>  </template>    <block>   <template></template>  </block>

模板的作用域

模板拥有自己的作用域,只能使用data传入的数据。