模板

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

定义模板

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

<!--my-template-->
<template name="msgItem">
  <view mc:for="{{messages}}">
    <view> Index: {{index}} </view>
    <view> Message: {{item.msg}} </view>
  </view>
</template>

使用模板

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

<!--template-->
<template is="msgItem" data="{{...item}}"/>
<import src="./my-template.mcml"></import>
Page({
  data: {
    item: {
      messages:[
        {
          msg: 'this is a template 1'
        },
        {
          msg: 'this is a template 2'
        }
      ]
    }
  }
})

is 属性支持 Mustache 语法,来动态决定具体需要渲染哪个模板:

<!--my-template-->
<template name="odd">
  <view> odd </view>
</template>
<template name="even">
  <view> even </view>
</template>
<!--template-->
<block mc:for="{{[1, 2, 3, 4, 5]}}">
    <template is="{{item % 2 == 0 ? 'even' : 'odd'}}"/>
</block>
<import src="./my-template.mcml"></import>

模板的作用域

模板拥有自己的作用域,只能使用 data 传入的数据以及模版定义文件中定义的 <mcs /> 模块。

results matching ""

    No results matching ""

    results matching ""

      No results matching ""