WXML template
Introduction
The suffix of the WXML file is .wxml. Open the pages/wxml/index.wxml file. Readers who have experience in HTML development should be familiar with this way of writing code. Simple WXML statements are very similar to HTML in syntax.
<!--pages/wxml/index.wxml-->
<text>pages/wxml/index.wxml</text><!-- Add comment here-->
<Tag name Attribute name 1="Attribute value 1" Attribute name 2="Attribute value 2" ...> ...</Tag name><text>hello world
<!--
text is not closed, resulting in a compile error:
VM148:2 ./pages/wxml/index.wxml
end tag missing, near text
> 1 | <text>hello world
| ^
--><!-- A simple text tag-->
<text>hello world</text>
<!-- view contains text tags -->
<view>
<text>hello world</text>
</view><image class="userinfo-avatar" src="./image/a.png" ></image>Data Binding
<!--pages/wxml/index.wxml-->
<text>Current time: {{time}}</text>// pages/wxml/index.js
Page({
/**
* Initial page data
*/
data: {
time: (new Date()).toString()
},
})<!--
{
time: (new Date()).toString()
}
-->
<text> Current time: {{time}}/text><!-- Correct coding -->
<text data-test="{{test}}"> hello world</text>
<!-- Wrong coding -->
<text data-test={{test}}> hello world </text ><!--
{
w: 'w',
W: 'W'
}
-->
<view>{{w}}</view>
<view>{{W}}</view>
<!-- Output
w
W
--><!--
{
var2: undefined,
var3: null,
var4: "var4"
}
-->
<view>{{var1}}</view>
<view>{{var2}}</view>
<view>{{var3}}</view>
<view>{{var4}}</view>
<!--
Output:
null
var4
-->Logical syntax
Ternary operation:
<!-- Display page content conditionally based on whether a is equal to 10 or not -->
<text>{{ a === 10? "variable a is equal to 10": "variable a is not equal to 10"}}</text><!--
{ a: 1, b: 2, c: 3 }
-->
<view> {{a + b}} + {{c}} + d </view>
<!-- Output 3 + 3 + d --><!--
{ name: 'world' }
-->
<view>{{"hello " + name}}</view>
<!-- Output hello world --><text>{{[1,2,3]}}</text>
<!-- Output 1,2,3 -->
<text>{{"hello world"}}</text>
<!-- Output hello world -->Conditional logic
In WXML, use wx:if="{{condition}}" to determine whether the code block needs to be rendered:
<view wx:if="{{condition}}"> True </view><view wx:if="{{length > 5}}"> 1 </view>
<view wx:elif="{{length > 2}}"> 2 </view>
<view wx:else> 3 </view><block wx:if="{{true}}">
<view> view1 </view>
<view> view2 </view>
</block>List Rendering
Use the wx:for control attribute on the component to bind an array, and you can use the data of each item in the array to repeatedly render the component. The default subscript variable name of the current item in the array is index, and the default variable name of the current item in the array is item, as shown in the following code:
<!-- `array` is an array -->
<view wx:for="{{array}}">
{{index}}: {{item.message}}
</view>
<!-- Corresponding script file
Page({
data: {
array: [{
message: 'foo',
}, {
message: 'bar'
}]
}
})
--><view wx:for="{{array}}" wx:for-index="idx" wx:for-item="itemName">
{{idx}}: {{itemName.message}}
</view><block wx:for="{{[1, 2, 3]}}">
<view> {{index}}: </view>
<view> {{item}} </view>
</block><switch wx:for="{{objectArray}}" wx:key="unique" > {{item.id}} </switch>
<button bindtap="switch"> Switch </button>
<button bindtap="addToFront"> Add to the front </button>
<switch wx:for="{{numberArray}}" wx:key="*this" > {{item}} </switch>
<button bindtap="addNumberToFront"> Add Number to the front </button>Page({
data: {
objectArray: [
{id: 5, unique: 'unique_5'},
{id: 4, unique: 'unique_4'},
{id: 3, unique: 'unique_3'},
{id: 2, unique: 'unique_2'},
{id: 1, unique: 'unique_1'},
{id: 0, unique: 'unique_0'},
],
numberArray: [1, 2, 3, 4]
},
switch: function(e) {
const length = this.data.objectArray.length
for (let i = 0; i < length; ++i) {
const x = Math.floor(Math.random() * length)
const y = Math.floor(Math.random() * length)
const temp = this.data.objectArray[x]
this.data.objectArray[x] = this.data.objectArray[y]
this.data.objectArray[y] = temp
}
this.setData({
objectArray: this.data.objectArray
})
},
addToFront: function(e) {
const length = this.data.objectArray.length
this.data.objectArray = [{id: length, unique: 'unique_' + length}].concat(this.data.objectArray)
this.setData({
objectArray: this.data.objectArray
})
},
addNumberToFront: function(e){
this.data.numberArray = [ this.data.numberArray.length + 1 ].concat(this.data.numberArray)
this.setData({
numberArray: this.data.numberArray
})
}
})Template
WXML provides templates (template), you can define code snippets in the template, and then call the name attribute in different places as the name of the template and then define the code snippet in <template/>, as shown in the following code:
<template name="msgItem">
<view>
<text> {{index}}: {{msg}} </text>
<text> Time: {{time}} </text>
</view>
</template><!--
item: {
index: 0,
msg: 'this is a template',
time: '2016-06-18'
}
-->
<template name="msgItem">
<view>
<text> {{index}}: {{msg}} </text>
<text> Time: {{time}} </text>
</view>
</template>
<template is="msgItem" data="{{...item}}"/>
<!-- Output
0: this is a template Time: 2016-06-18
-->
<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>
<!-- Output
odd
even
odd
even
odd
-->Reference
WXML provides two file reference methods: import and include.
<!-- item.wxml -->
<template name="item">
<text>{{text}}</text>
</template><import src="item.wxml"/>
<template is="item" data="{{text: 'forbar'}}"/><!-- A.wxml -->
<template name="A">
<text> A template </text>
</template><!-- B.wxml --><import src="a.wxml"/><template name="B"> <text> B template </text></template><!-- C.wxml -->
<import src="b.wxml"/>
<template is="A"/> <!-- A warning will be triggered here because template A is not defined in b -->
<template is="B"/><!-- index.wxml -->
<include src="header.wxml"/>
<view> body </view>
<include src="footer.wxml"/><!-- header.wxml -->
<view> header </view><!-- footer.wxml -->
<view> footer </view>