Mini App Document Mini App Document
  • Start
  • Configuration
  • Framework
  • Custom Components
  • Basic ability
  • Configuration
  • Interface
  • FXML syntax
  • FXS syntax
API
Components
IDE
  • Developer
  • Operator
  • Start
  • Configuration
  • Framework
  • Custom Components
  • Basic ability
  • Configuration
  • Interface
  • FXML syntax
  • FXS syntax
API
Components
IDE
  • Developer
  • Operator
  • Configuration

  • Interface

  • FXML syntax

    • Introduction to FXML
    • Data binding
    • List rendering
    • Conditional rendering
    • Template
      • 1. Define the template
      • 2. Use templates
      • 3. The scope of the template
    • Quote
  • FXS syntax

  • Framework
  • FXML syntax
2022-08-09
Directory

Template

FXML provides templates that can be defined in a template and then called in different places.

# 1. Define the template

Use the name attribute as the name of the template. Then define the code snippet within '' as follows:

<!--
  index: int
  msg: string
  time: string
-->
<template name="msgItem">
  <view>
    <text> {{index}}: {{msg}} </text>
    <text> Time: {{time}} </text>
  </view>
</template>

# 2. Use templates

Using the is attribute, declare the template you want to use, and then pass in the data that the template requires, such as:

<template is="msgItem" data="{{...item}}"/>
Page({
  data: {
    item: {
      index: 0,
      msg: 'this is a template',
      time: '2016-09-15'
    }
  }
})

The is property can use the Mustache syntax to dynamically decide which template to render:

<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>

# 3. The scope of the template

Templates have their own scope and can only use data incoming data and the ''' modules defined in the template definition file.

Last update: 2022/08/11, 20:42:12
Conditional rendering
Quote

← Conditional rendering Quote→

Copyright © 2020-2024 Neuxnet
  • Follow System
  • Light Mode
  • Dark Mode
  • Reading Mode