WXML提供模板(template),可以在模板中定義代碼片段,然后在不同的地方調(diào)用。
使用name屬性,作為模板的名字。然后在<template/>
內(nèi)定義代碼片段,如:
<!--
index: int
msg: string
time: string
-->
<template name="msgItem">
<view>
<text> {{index}}: {{msg}} </text>
<text> Time: {{time}} </text>
</view>
</template>
使用is屬性,聲明需要的使用的模板,然后將模板所需要的data傳入,如:
<template is="msgItem" data="{{...item}}"/>
Page({
data: {
item: {
index: 0,
msg: 'this is a template',
time: '2016-09-15'
}
}
})
is屬性可以使用Mustache語法,來動態(tài)決定具體需要渲染哪個模板:
<template name="odd">
<view> odd </view>
</template>
<template name="even">
<view> even </view>
</template>
<block wx:for="{{[1, 2, 3, 4, 5]}}">
<template is="{{item % 2 == 0 ? 'even' : 'odd'}}"/>
</block>
模板擁有自己的作用域,只能使用data傳入的數(shù)據(jù)。
更多微信小程序開發(fā)教程,可以關(guān)注。KESION 科汛軟件
KESION 科汛軟件是國內(nèi)領(lǐng)先的在線教育軟件及私域社交電商軟件服務(wù)提供商,長期專注于為企業(yè)提供在線教育軟件及社交電商SaaS平臺解決方案。
公司核心產(chǎn)品云開店SaaS社交電商服務(wù)平臺、在線教育SaaS服務(wù)平臺、教育企業(yè)數(shù)字化SaaS云平臺、企微營銷助手、私有化獨立部署品牌網(wǎng)校和在線教育咨詢等。KESION 不斷通過技術(shù)創(chuàng)新,提供產(chǎn)品和服務(wù),助力企業(yè)向數(shù)字化轉(zhuǎn)型,通過科技驅(qū)動商業(yè)革新,讓商業(yè)變得更智慧!
wx:for 在組件上使用 wx:for 控制屬性綁定一個數(shù)組,即可使用數(shù)組中各項的數(shù)據(jù)重復(fù)渲染該組件。 默認數(shù)組的當(dāng)前項的下標(biāo)變量名默認為 index ,數(shù)組當(dāng)前項的變量名默認為 item view
引用 WXML提供兩種文件引用方式 import 和 include 。 import import 可以在該文件中使用目標(biāo)文件定義的 template ,如: 在item.wxml中定義了一個叫 i