Uses a markup language similar to HTML to build a page consisting of basic elements, custom elements, and events.
A template can contain only one root node, for example, div. Do not configure multiple root nodes under template or use block as the root node.
<template>
<text> {{message}} </text>
</template>
<script>
module.exports= {
data: {
message: 'Hello World!'
}
}
</script> A template of the inline style supports data binding. The supported data binding style is as follows:
{{'height:'+height+'px'}} <template>
<div>
<!-- Normal format -->
<div onclick="press"></div>
<!-- Short format -->
<div @click="press"></div>
</div>
</template>
<script>
module.exports= {
press: function(e) {
this.title = 'Hello'
}
}
</script> The syntax for event callback is as follows ({{}} can be omitted):
When the callback function is called, the evt parameter is automatically added to the end of the parameter list. The evt parameter is used to access data related to the callback event, for example, tap coordinates x and y of the tap event. (For details about the data content, please refer to the description of the element callback event.)
<template>
<div class="demo-page">
<text for="{{list}}" onclick="handle($idx,$item)">{{$item}}</text>
</div>
</template>
<style>
.demo-page{
flex-direction: column;
align-items: center;
}
</style>
<script>
export default {
private: {
list:[1,2,3,4,5]
},
handle(idx,item,evt){
console.log("idx:"+idx);
console.log("item: "+item);
console.log('evt: '+JSON.stringify(evt));
}
}
</script> The following shows the effect after you click the last text.

<template>
<div class="demo-page">
<text for="{{list}}" onclick="handle(total,$item)">{{$item}}</text>
</div>
</template>
<style>
.demo-page{
flex-direction: column;
align-items: center;
}
</style>
<script>
export default {
private: {
list:[1,2,3,4,5],
total:0
},
handle(total,num,evt){
console.log("total:"+total);
console.log("num: "+num);
console.log('evt: '+JSON.stringify(evt));
}
}
</script> The following figure shows the final effect.

<template>
<div class="list-container">
<div for="{{list}}" tid="id">
<text>{{$idx}}</text>
<text>{{$item.id}}</text>
</div>
</div>
</template>
<script>
module.exports= {
data: {
list: [{id:1}, {id:2}]
}
}
</script> In the tag, tid is used to specify the field name that indicates the unique ID of an array element. If the ID is not specified, the array index ($idx) is used as the unique ID by default. The function of tid is to reuse the element node and improve the redrawing efficiency of the for loop. For example, tid="id" indicates that $item.id is the unique ID of an array element. The for loop syntax is as follows ({{}} can be omitted):
There are two types of syntax for conditional rendering: if/elif/else and show. The difference is as follows: If the condition defined by if is not met, the element is removed from the VDOM. For show, the element is only invisible during rendering, and still exists in the VDOM. The if, elif, and else statements must be neighboring siblings. Otherwise, the compilation fails.
<template>
<div>
<text if="{{show}}"> Hello-1 </text>
<text elif="{{display}}"> Hello-2 </text>
<text else> Hello-3 </text>
</div>
</template>
<script>
module.exports= {
data: {
show: false,
display: true
}
}
</script> The effect of show is equivalent to that of display=none. Currently, it applies only to native elements and does not take effect for custom elements. For custom elements, related parameters can be imported through props, and show can be used inside the elements to control the visibility of elements.
<template>
<div>
<text show="{{visible}}"> Hello </text>
</div>
</template>
<script>
module.exports= {
data: {
visible: false
}
}
</script> For more flexible loop or conditional rendering, <block> can be used. Currently, <block> only supports the for and if-elif-else statements. If no attribute is specified in a <block> section, this section is treated as a "transparent" node in a build and its subnodes are added to its parent <block> node.
<template>
<list>
<block for="cities">
<list-item type="city">
<text>{{$item.name}}</text>
</list-item>
<block for="$item.spots" if="false">
<list-item type="spot">
<text>{{$item.address}}</text>
</list-item>
</block>
</block>
</list>
</template>
<script>
module.exports= {
data: {
cities: [
{name:"Beijing", spots:[{name:"XXX",address:"XXX"},{name:"XXX",address:"XXX"}]},
{name:"Shanghai", spots:[{name:"XXX",address:"XXX"},{name:"XXX",address:"XXX"}]}
]
}
}
</script> Elements are imported through the import tag.
Sample code:
<import name="comp-part1" src="../part1/part1.ux"></import>
<template>
<div class="tutorial-page">
<text class="tutorial-title">Import an Element</text>
<comp-part1></comp-part1>
</div>
</template>
<style>
.tutorial-page {
flex-direction: column;
padding: 20px 10px;
}
.tutorial-title {
font-weight: bold;
}
</style>
<script>
// Parent element.
export default {
private: {},
onInit() {
this.$page.setTitleBar({text:'Import an Element'})
}
}
</script>
To implement the data communication, declare the name of the attribute whose data is to be received in props of the subelement. In the parent element, declare the data to be passed in the subelement tag.
Parent element:
<import name="comp-part1" src="../part1/part1.ux"></import>
<!-- Parent element -->
<template>
<div class="parent-demo">
<comp-part1 say="{{say}}" prop-object="{{obj}}"></comp-part1>
</div>
</template>
<script>
export default {
private: {
say: 'hello',
obj: {
name: 'child-demo'
}
}
}
</script> Subelement:
<!-- Subelement -->
<template>
<div class="child-demo">
<text class="title">Subelement:</text>
<text>{{ say }}</text>
<text>{{ propObject.name }}</text>
</div>
</template>
<script>
export default {
props: ['say', 'propObject'],
onInit() {
console.info (`Data passed externally: `, this.say, this.propObject)
}
}
</script> The communication is implemented in the following methods:
Samples:
Parent element:
<!-- Parent element -->
<import name="comp1" src="./com/comp1.ux"></import>
<import name="comp2" src="./com/comp2.ux"></import>
<import name="comp3" src="./com/comp3.ux"></import>
<template>
<div class="parent-demo">
<text>count of the parent element:{{ count }}</text>
<comp1 count="{{count}}" onemit-evt="emitEvt"></comp1>
<text>num of the parent element:{{ num }}</text>
<comp2 num="{{num}}"></comp2>
<text>age:{{ age }}</text>
<input type="button" onclick="evtTypeEmit" value="Trigger $broadcast()"></input>
<comp3></comp3>
</div>
</template>
<script>
export default {
private: {
count: 20,
num: 20,
age: 18
},
onInit() {
this.$on('dispatchEvt', this.dispatchEvt) },
emitEvt(evt) {
this.count = evt.detail.count},
dispatchEvt(evt) {
this.num = evt.detail.num },
evtTypeEmit() {
this.$broadcast('broadevt', {
age: 19}) },}
</script> Subelement A:
<!-- comp1 -->
<template>
<div class="child-demo">
<text>count of subelement A:{{compCount}}</text>
<input type="button" onclick="addHandler" value="add"></input>
</div>
</template>
<script>
export default {
props: ['count'],
data() {
return {
compCount: this.count
}
},
addHandler() {
this.compCount++
this.$emit('emitEvt', {
count: this.compCount
})
},
}
</script> Subelement B:
<!-- comp2 -->
<template>
<div class="child-demo">
<text>num of subelement B:{{compNum}}</text>
<input type="button" onclick='delHandler' value='del'></input>
</div>
</template>
<script>
export default {
props: ['num'],
data(){
return{
compNum:this.num
}
},
delHandler(){
this.compNum --
this.$dispatch('dispatchEvt',{
num:this.compNum
})
},
}
</script> Subelement C:
<!-- comp3 -->
<template>
<div class="child-demo">
<text>age of subelement C:{{compAge}}</text>
</div>
</template>
<script>
export default {
props: [],
data() {
return {
compAge: null
}
},
onInit() {
this.$on('broadevt', this.broadevt)
},
broadevt(evt) {
this.compAge = evt.detail.age
}
}
</script> In an element, a tag is used to define the insertion position of a subelement. Example:
The template of the com-a element is defined as follows:
<template>
<div>
<text>header</text>
<slot>default content</slot>
<text>footer</text>
<div>
</template> To use the comA element on a page, write code as follows:
<import name="comp-a" src="./comp-a"></import>
<template>
<com-a>
<text>body</text>
</com-a>
</template> The page rendering of the comA element is as follows:
<div> <text>header</text> <text>body</text> <text>footer</text> </div>
Before dynamic elements are available, you usually use the if-elif-else statement to implement requirements. With dynamic elements, you can flexibly determine the custom elements to be rendered in the template.
As shown in the following code, the logical element component and the corresponding is attribute are used together to determine the element to be rendered during running.
<import name="part1" src="./compA"></import>
<import name="part2" src="./compB"></import>
<import name="part3" src="./compC"></import>
<template>
<div class="container">
<div class="page-title-wrap">
<text class="page-title">{{componentName}}</text>
</div>
<div class="tutorial-page">
<div class="item-content">
<component is="{{who}}"></component>
</div>
<input type="button" class="btnComponent" onclick="onchangeComponent"
value="changeComponent" />
</div>
</div>
</template> <script>
export default {
data: {
componentName: 'Component dynamic component',
who: 'part1'
},
onchangeComponent() {
if (this.who === 'part1') {
this.who = 'part2'
} else if (this.who === 'part2') {
this.who = 'part3'
} else {
this.who = 'part1'
}
}
}
</script> In hap-toolkit 0.4.0 and later versions, you can customize a filter to format texts.
A filter is a function defined in the object exported from a page, for example, capitalize.
<script>
export default {
private: {
message: 'hello'
},
capitalize(value) {
if (!value) return ''
value = value.toString()
return value.charAt(0).toUpperCase() + value.slice(1)
}
}
</script> To use a filter, you should add it to the end of the expression and separate it with the expression with a pipe. Similar to the vue filter, the format is as follows:
{{ message | capitalize }} In the previous example, the filter function will receive the expression value as the first parameter, that is, the capitalize filter will use the received value of the message expression as the first parameter. The complete sample code is as follows:
<template>
<div>
<text>{{ message | capitalize }}</text>
</div>
</template>
<script>
export default {
private: {
message: 'hello'
},
capitalize(value) {
if (!value) return ''
value = value.toString()
return value.charAt(0).toUpperCase() + value.slice(1)
}
}
</script> Filters can also be used in two other ways: connecting filters in series and receiving multiple parameters.
In the sample code below, filterA is defined as a function receiving only one parameter. The value of the message expression will be passed to filterA as a parameter. Then, filterB also defined as a function receiving only one parameter is called and the result of filterA is passed to filterB as a parameter.
{{ message | filterA | filterB }} In the sample code below, filterA is defined as a function receiving three parameters. The value of the message expression is the first parameter, the common string arg1 is the second, and the value of the arg2 expression is the third.
{{ message | filterA('arg1', arg2) }}