# HML - [HML Page Structure](#en-us_topic_0000001059340504_section1062764791514) - [Data Binding](#en-us_topic_0000001059340504_s8821c158917c48098219013e69129d1b) - [Event Binding](#en-us_topic_0000001059340504_s30850b61328e4359910467ab33b3e07d) - [Loop Rendering](#en-us_topic_0000001059340504_sb777d6d807fa43fea9be400b2600425b) - [Conditional Rendering](#en-us_topic_0000001059340504_sf7f6eab2105a4030a1f34149417d6fc5) - [Logic Control Block](#en-us_topic_0000001059340504_s185169def14340fcbb12c3375cb0f0bb) - [Template Reference](#en-us_topic_0000001059340504_section1388145672114) The OpenHarmony Markup Language \(HML\) is an HTML-like language that allows you to build pages based on components and events. Pages built using HML have advanced capabilities such as logic control, data binding, event binding, loop rendering, and conditional rendering. ## HML Page Structure ```
Image Show
``` ## Data Binding ```
{{content[1]}}
``` ``` // xxx.js export default { data: { content: ['Hello World!', 'Welcome to my world!'] }, changeText: function() { this.content.splice(1, 1, this.content[0]); } } ``` >![](public_sys-resources/icon-note.gif) **NOTE:** >- To make the array data modification take effect, use the **splice** method to change array items. >- ECMAScript 6.0 syntax is not supported in HML. ## Event Binding The callback bound to an event receives an event object parameter, which can be used to obtain the event information. ```
``` ``` // xxx.js export default { data: { obj: '', }, clickfunc: function(e) { this.obj = 'Hello World'; console.log(e); }, } ``` - Example Code ```
{{count}}
``` ``` /* xxx.js */ export default { data: { count: 0 }, increase() { this.count++; }, decrease() { this.count--; }, multiply(multiplier) { this.count = multiplier * this.count; } }; ``` ``` /* xxx.css */ .container { display: flex; flex-direction: column; justify-content: center; align-items: center; left: 0px; top: 0px; width: 454px; height: 454px; } .title { font-size: 30px; text-align: center; width: 200px; height: 100px; } .box { width: 454px; height: 200px; justify-content: center; align-items: center; flex-wrap: wrap; } .btn { width: 200px; border-radius: 0; margin-top: 10px; margin-left: 10px; } ``` ## Loop Rendering ```
{{$idx}}.{{$item.name}}
{{$idx}}.{{value.name}}
{{index}}.{{value.name}}
``` ``` // xxx.js export default { data: { array: [ {id: 1, name: 'jack', age: 18}, {id: 2, name: 'tony', age: 18}, ], }, changeText: function() { if (this.array[1].name === "tony"){ this.array.splice(1, 1, {id:2, name: 'Isabella', age: 18}); } else { this.array.splice(2, 1, {id:3, name: 'Bary', age: 18}); } }, } ``` The **tid** attribute accelerates the **for** loop and improves the re-rendering efficiency when data in a loop changes. The **tid** attribute specifies the unique ID of each element in the array. If it is not specified, the index of each element in the array is used as the ID. For example, **tid="id"** indicates that the **id** attribute of each element is its unique ID. The **for** loop supports the following statements: - for="array": **array** is an array object, whose element variable is **$item** by default. - for="v in array": **v** is a custom element variable, whose index is **$idx** by default. - for="\(i, v\) in array": **i** indicates the element index, and **v** indicates the element variable. All elements of the array object will be looped through. >![](public_sys-resources/icon-note.gif) **NOTE:** >- Each element in the array must have the data attribute specified by **tid**. Otherwise, an exception may occur. >- The attribute specified by **tid** in the array must be unique. Otherwise, performance loss occurs. In the above example, only **id** and **name** can be used as **tid** because they are unique fields. >- The **tid** field does not support expressions. ## Conditional Rendering There are two ways to implement conditional rendering: **if-elif-else** or **show**. In **if-elif-else**, when the **if** statement evaluates to **false**, the component is not built in the VDOM and is not rendered. For **show**, when show is **false**, the component is not rendered but is built in the VDOM. In addition, the **if-elif-else** statements must be used in sibling nodes. Otherwise, the compilation fails. The following example uses both ways to implement conditional rendering: ```
Hello-One Hello-Two Hello-World
``` ``` // xxx.css .container{ flex-direction: column; align-items: center; } .btn{ width: 280px; font-size: 26px; margin: 10px 0; } ``` ``` // xxx.js export default { data: { show: false, display: true, }, toggleShow: function() { this.show = !this.show; }, toggleDisplay: function() { this.display = !this.display; } } ``` In the optimized rendering \(**show**\), if **show** is **true**, the node is rendered properly; if it is **false**, the display style will be **none**. ```
Hello World
``` ``` // xxx.css .container{ flex-direction: column; align-items: center; } .btn{ width: 280px; font-size: 26px; margin: 10px 0; } ``` ``` // xxx.js export default { data: { visible: false, }, toggle: function() { this.visible = !this.visible; }, } ``` >![](public_sys-resources/icon-note.gif) **NOTE:** >Do not use **for** and **if** attributes at the same time in an element. ## Logic Control Block **** makes loop rendering and conditional rendering more flexible. A **** will not be compiled as a real component. Note that the **** tag supports only the **for** and **if** attributes. ``` {{$item.name}} {{$item.color}} ``` ``` // xxx.js export default { data: { glasses: [ {name:'sunglasses', kinds:[{name:'XXX',color:'XXX'},{name:'XXX',color:'XXX'}]}, {name:'nearsightedness mirror', kinds:[{name:'XXX',color:'XXX'}]}, ], }, } ``` ## Template Reference HML supports using elements to reference template files. For details, see [Custom Components](basic-usage.md#EN-US_TOPIC_0000001162494627). ```
Name: {{name}} Age: {{age}}
``` ```
```