js-framework-syntax-hml.md 10.6 KB
Newer Older
G
ge-yafang 已提交
1
# HML
M
mamingshuai 已提交
2 3


G
ge-yafang 已提交
4 5 6 7 8
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

M
mamingshuai 已提交
9 10 11 12 13 14 15 16 17 18 19

```
<!-- xxx.hml -->
<div class="item-container">
  <text class="item-title">Image Show</text>
  <div class="item-content">
    <image src="/common/xxx.png" class="image"></image>
  </div>
</div>
```

G
ge-yafang 已提交
20 21 22

## Data Binding

M
mamingshuai 已提交
23 24 25 26 27 28 29 30

```
<!-- xxx.hml -->
<div onclick="changeText">
  <text> {{content[1]}} </text>
</div>
```

G
ge-yafang 已提交
31

M
mamingshuai 已提交
32 33 34 35 36 37 38 39 40 41 42 43
```
// xxx.js
export default {
  data: {
    content: ['Hello World!', 'Welcome to my world!']
  },
  changeText: function() {
    this.content.splice(1, 1, this.content[0]);
  }
}
```

G
ge-yafang 已提交
44 45 46 47
> ![icon-note.gif](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 (ES6) syntax is not supported in HML.
M
mamingshuai 已提交
48 49


G
ge-yafang 已提交
50 51 52
## Common Event Binding

Events are bound to components through 'on' or '@'. When a component triggers an event, the corresponding event processing function in the .js file is executed.
M
mamingshuai 已提交
53

Z
zengyawen 已提交
54
Events can be written in the following formats:
M
mamingshuai 已提交
55

G
ge-yafang 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
- funcName: name of the event callback, which is implemented by defining the corresponding function in the .js file.

- funcName(a,b): function parameters, such as a and b, which can be constants, or variables defined in data in the .js file. Do not add the prefix this. to variables.

- Example
  
  ```
  <!-- xxx.hml -->
  <div class="container">
      <text class="title">{{count}}</text>
      <div class="box">
          <input type="button" class="btn" value="increase" onclick="increase" />
          <input type="button" class="btn" value="decrease" @click="decrease" />
      <!-- Pass additional parameters. -->
          <input type="button" class="btn" value="double" @click="multiply(2)" />
          <input type="button" class="btn" value="decuple" @click="multiply(10)" />
          <input type="button" class="btn" value="square" @click="multiply(count)" />
      </div>
  </div>
  ```

  
  ```
  // xxx.js
  export default {
    data: {
      count: 0
    },
    increase() {
      this.count++;
    },
    decrease() {
      this.count--;
    },
    multiply(multiplier) {
      this.count = multiplier * this.count;
M
mamingshuai 已提交
92
    }
G
ge-yafang 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
  };
  ```

  
  ```
  /* 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;
  }
  ```
M
mamingshuai 已提交
129 130


G
ge-yafang 已提交
131
## Bubbling Event Binding<sup>5+</sup>
Z
zengyawen 已提交
132 133 134

Bubbling event binding covers the following:

G
ge-yafang 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
- Bind an event callback for event bubbling: on:{event}.bubble. on:{event} is equivalent to on:{event}.bubble.

- Bind an event callback, but stop the event from bubbling upwards: grab:{event}.bubble. grab:{event} is equivalent to grab:{event}.bubble.
  > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
  > For details about bubbling events, see [Universal Events](../reference/arkui-js/js-components-common-events.md)

- Example
  
  ```
  <!-- xxx.hml -->
  <div>
     <!-- Bind an event callback for event bubbling.5+ -->
      <div on:touchstart.bubble="touchstartfunc"></div>
      <div on:touchstart="touchstartfunc"></div>
      <!-- Bind an event callback, but stop the event from bubbling upwards. 5+ -->
      <div grab:touchstart.bubble="touchstartfunc"></div>
      <div grab:touchstart="touchstartfunc"></div>
      <!-- Bind an event callback for event bubbling.6+ -->
      <div on:click.bubble="clickfunc"></div>
      <div on:click="clickfunc"></div>
      <!-- Bindan event callback, but stop the event from bubbling upwards.6+ -->
      <div grab:click.bubble="clickfunc"></div>
      <div grab:click="clickfunc"></div>
  </div>
  ```
  
  
  ```
  // xxx.js
  export default {
      clickfunc: function(e) {
          console.log(e);
      },
      touchstartfuc: function(e) {
          console.log(e);
      },
  }
  ```
Z
zengyawen 已提交
173

G
ge-yafang 已提交
174 175
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
> Events bound using a traditional statement (such as onclick) will bubble only when the API version in use is 6 or later.
Z
zengyawen 已提交
176

G
ge-yafang 已提交
177
## Capturing Event Binding<sup>5+</sup>
Z
zengyawen 已提交
178 179 180 181 182

Touch events can be captured. In the capture phase, which precedes the bubbling phase, an event starts from the parent component to the child component.

Event capturing binding includes:

G
ge-yafang 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
- Bind an event callback for event capturing: on:{event}.capture.

- Bind an event callback, but stop the event from being captured during downward transfer: grab:{event}.capture.

- Example
  
  ```
  <!-- xxx.hml -->
  <div>
      <!-- Bind an event callback for event capturing.5+ -->    <div on:touchstart.capture="touchstartfunc"></div>
      <!-- Bind an event callback, but stop the event from being captured during downward transfer.5+ -->
      <div grab:touchstart.capture="touchstartfunc"></div>
  </div>
  ```

  
  ```
  // xxx.js
  export default {
      touchstartfuc: function(e) {
          console.log(e);
      },
  }
  ```

Z
zengyawen 已提交
208

G
ge-yafang 已提交
209
## Loop Rendering
Z
zengyawen 已提交
210

M
mamingshuai 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

```
<!-- xxx.hml -->
<div class="array-container">
  <!-- div loop rendering -->
  <!-- By default, $item indicates the element in the array, and $idx indicates the index of the element in the array. -->
  <div for="{{array}}" tid="id" onclick="changeText">
    <text>{{$idx}}.{{$item.name}}</text>
  </div>
  <!-- Define the name for an element variable. -->
  <div for="{{value in array}}" tid="id" onclick="changeText">    
    <text>{{$idx}}.{{value.name}}</text>
  </div>
  <!-- Define an element variable and its index name. -->
  <div for="{{(index, value) in array}}" tid="id" onclick="changeText">    
    <text>{{index}}.{{value.name}}</text>
  </div>
</div>
```

G
ge-yafang 已提交
231

M
mamingshuai 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
```
// 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});
    }
  },
}
```

G
ge-yafang 已提交
251 252 253 254 255 256 257
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.
M
mamingshuai 已提交
258

G
ge-yafang 已提交
259
- for="v in array": v is a custom element variable, whose index is $idx by default.
M
mamingshuai 已提交
260

G
ge-yafang 已提交
261
- 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.
M
mamingshuai 已提交
262

G
ge-yafang 已提交
263 264 265 266 267 268
> ![icon-note.gif](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.
M
mamingshuai 已提交
269 270


G
ge-yafang 已提交
271 272 273
## 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:
M
mamingshuai 已提交
274 275 276 277 278 279 280


```
<!-- xxx.hml -->
<div class="container">
  <button class="btn" type="capsule" value="toggleShow" onclick="toggleShow"></button>
  <button class="btn" type="capsule" value="toggleDisplay" onclick="toggleDisplay"></button>
Z
zengyawen 已提交
281 282
  <text if="{{visible}}"> Hello-TV </text>
  <text elif="{{display}}"> Hello-Wearable </text>
M
mamingshuai 已提交
283 284 285 286
  <text else> Hello-World </text>
</div>
```

G
ge-yafang 已提交
287

M
mamingshuai 已提交
288
```
Z
zengyawen 已提交
289
/* xxx.css */
M
mamingshuai 已提交
290 291 292 293 294 295 296 297 298 299 300
.container{
  flex-direction: column;
  align-items: center;
}
.btn{
  width: 280px;
  font-size: 26px;
  margin: 10px 0;
}
```

G
ge-yafang 已提交
301

M
mamingshuai 已提交
302 303 304 305
```
// xxx.js
export default {
  data: {
Z
zengyawen 已提交
306
    visible: false,
M
mamingshuai 已提交
307 308 309
    display: true,
  },
  toggleShow: function() {
Z
zengyawen 已提交
310
    this.visible = !this.visible;
M
mamingshuai 已提交
311 312 313 314 315 316 317
  },
  toggleDisplay: function() {
    this.display = !this.display;
  }
}
```

G
ge-yafang 已提交
318 319
In the optimized rendering (show), if show is true, the node is rendered properly; if it is false, the display style will be none.

M
mamingshuai 已提交
320 321 322 323 324 325 326 327 328

```
<!-- xxx.hml -->
<div class="container">
  <button class="btn" type="capsule" value="toggle" onclick="toggle"></button>
  <text show="{{visible}}" > Hello World </text>
</div>
```

G
ge-yafang 已提交
329

M
mamingshuai 已提交
330
```
Z
zengyawen 已提交
331
/* xxx.css */
M
mamingshuai 已提交
332 333 334 335 336 337 338 339 340 341 342
.container{
  flex-direction: column;
  align-items: center;
}
.btn{
  width: 280px;
  font-size: 26px;
  margin: 10px 0;
}
```

G
ge-yafang 已提交
343

M
mamingshuai 已提交
344 345 346 347 348 349 350 351 352 353 354 355
```
// xxx.js
export default {
  data: {
    visible: false,
  },
  toggle: function() {
    this.visible = !this.visible;
  },
}
```

G
ge-yafang 已提交
356 357
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**:
> Do not use for and if attributes at the same time in an element.
M
mamingshuai 已提交
358

G
ge-yafang 已提交
359 360 361
## Logic Control Block

<block> makes loop rendering and conditional rendering more flexible. A <block> will not be compiled as a real component. **NOTE** that the <block> tag supports only the for and if attributes.
M
mamingshuai 已提交
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379


```
<!-- xxx.hml -->
<list>
  <block for="glasses">
    <list-item type="glasses">
      <text>{{$item.name}}</text>
    </list-item>
    <block for="$item.kinds">
      <list-item type="kind">
        <text>{{$item.color}}</text>
      </list-item>
    </block>
  </block>
</list>
```

G
ge-yafang 已提交
380

M
mamingshuai 已提交
381 382 383 384 385 386 387 388 389 390 391 392
```
// 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'}]},
    ],
  },
}
```

G
ge-yafang 已提交
393 394 395
## Template Reference

HML supports using elements to reference template files. For details, see Custom Components.
M
mamingshuai 已提交
396 397 398 399 400 401 402 403 404 405


```
<!-- template.hml -->
<div class="item"> 
  <text>Name: {{name}}</text>
  <text>Age: {{age}}</text>
</div>
```

G
ge-yafang 已提交
406

M
mamingshuai 已提交
407 408 409 410 411 412 413
```
<!-- index.hml -->
<element name='comp' src='../../common/template.hml'></element>
<div>
  <comp name="Tony" age="18"></comp>
</div>
```