W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
TTML提供模板(template),可以在模板中定義代碼片段,然后在不同的地方調(diào)用。
使用 name 屬性,作為模板的名字。然后在內(nèi)定義代碼片段,如:
<!--
index: int
msg: string
time: string
-->
<template name="msgItem">
<view>
<text> {{index}}: {{msg}} </text>
<text> Time: {{time}} </text>
</view>
</template>
使用 is 屬性,聲明需要的使用的模板,然后將模板所需要的 data 傳入,如:
<!-- index.html -->
<template is="msgItem" data="{{...item}}"/>
// index.js
Page({
data: {
item: {
index: 0,
msg: 'this is a template',
time: '2016-09-15'
}
}
})
還可以動態(tài)決定具體需要渲染哪個模板:
<template name="odd">
<view> odd </view>
</template>
<template name="even">
<view> even </view>
</template>
<block tt:for="{{[1, 2, 3, 4, 5]}}">
<template is="{{item % 2 == 0 ? 'even' : 'odd'}}"/>
</block>
模板擁有自己的作用域,只能使用 data 傳入的數(shù)據(jù)。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: