# Gadget code composition

View the project of the gadget, and you can see that the project code contains files in the following formats. This article mainly introduces the role of these files.

- JSON configuration files with `.json` suffix
- TTML template files with `.ttml` extension
- TTSS style files with `.ttss` extension
- JS script logic files with `.js` suffix

## JSON configuration

There is an app.json and a project.config.json file in the root directory of the project. The purpose of the two is as follows.

- Gadget configuration app.json

app.json contains the global configuration of the current gadget, including all page paths of the gadget, page style configuration, header title, bottom tab, etc. The project generated with the development tool contains a default app.json file, through which you can configure some global properties of the gadget, declare all the pages of the gadget, and at the same time, if the gadget can run on the PC side, it can also Configure the default startup page and so on in the file.

- Project configuration project.config.json

The project configuration file mainly contains some information for the configuration of the Gadget project, such as project name, App ID, project syntax, compilation configuration, etc. These contents can be generated through developer tools when starting to create a project, and developers can also modify and configure them as needed.

For details and specific parameters of the JSON configuration file, see [Configuration Gadget](https://open.feishu.cn/document/uYjL24iN/uEDNuEDNuEDN).

## TTML template

TTML is used to describe the structure of the current page. An example TTML file is as follows:

```html
<view class="container">
   <view class="user" tt:if="{{hasUser}}">
     <text class="text">Hello World</text>
      <button bindtap="tap">Button</button>
   </view>
</view>
```

TTML works like HTML. TTML is composed of tags, attributes, etc., but it also differs from HTML in the following points.

- Tab names are different. TTML provides tags such as `view`, `button`, and `text`. These tags are the basic capabilities packaged by the gadget for you. In addition, the gadget also provides component capabilities such as maps, videos, and audio.
- Added attributes like `tt:if` and expressions in `{{ }}` format.
- TTML is used as a template syntax to describe the relationship between state and interface structure, and realize the separation of rendering and logic. The framework of the gadget follows this logic. For example, if you need to click a button to display `Hello World` on the interface, the TTML syntax example is as follows.

```html
   <view class="container">
     <view class="userinfo">
       <button bindtap="showMessage">Click the button to display Message</button>
     </view>
     <view class="textinfo" tt:if="{{isShowMessage}}">
       <text class="text">{{message}}</text>
     </view>
   </view>
   ```

At this point, JS only needs to manage the state.

```js
     const app = getApp()
     Page({
       data: {
         message: 'Hello World',
         isShowMessage: false,
       },
       showMessage() {
         this.setData({
           isShowMessage: true,
         })
       },
     })
     ```

Data binding refers to binding a variable to a label through the syntax of `{{ }}`. Only through data binding is not enough to fully describe the relationship between the state and the page, therefore, it is also necessary to provide some logical judgment and loop traversal functions, such as `if / else`, `for` and other capabilities. In gadget, these control capabilities are expressed with attributes beginning with `tt:`.

A detailed introduction to TTML files can be found in [TTML](https://open.feishu.cn/document/uYjL24iN/ugzNugzNugzN).

## TTSS style

TTSS has most of the features of CSS, and the TTSS of the gadget has also made some expansions and modifications based on the basic capabilities, as detailed below.

- Added a dynamic dimension unit.
- When writing a CSS layout, the display of the style will be affected by the screen width and device pixels of the device, so the gadget will use some tricks to convert the pixel unit. TTSS supports the new size unit rpx at the bottom layer. You can skip the conversion step and hand it over to the bottom layer of the gadget for conversion.

Due to the floating-point calculations used in the conversion, the calculation results may deviate slightly from the expected results.

- Provides global styles and local styles.
- The same concept as app.json and page.json, you can write an app.ttss as a global style, which will be applied to all pages of the current gadget, while the local page style page.ttss will only take effect on the current page.
- TTSS only supports some CSS selectors.

For a detailed introduction to TTSS files, please refer to [TTSS](https://open.feishu.cn/document/uYjL24iN/uYDOuYDOuYDO).

## JS interaction logic

A small program not only needs pages, but also needs to interact with users. For example, responding to the user's click, obtaining the user's location, etc. In gadget, you can respond to user actions through JS files. The sample code snippet is as follows:

```html
<view class="container">
   <view class="model">
     <text class="info">{{message}}</text>
     <button bindtap="showModel">Display device model</button>
   </view>
</view>
```

When clicking the `button` button, if you want to display the model of the user's current phone, you can declare an attribute `bindtap` on the `button`, and then declare the `showModel` method in the JS file to respond to the click operation. The sample code snippet is as follows:

```js
const app = getApp()
Page({
   data: {
     message: '',
   },
   showModel() {
     // Get device information
     tt.getSystemInfo({
       success: (res) => {
         // Call this.setData for state management
         this.setData({
           message: res.model,
         })
       },
     })
   }
})
```

In addition, you can also call the client in JS to provide rich APIs for the gadget, and use these APIs to conveniently call the capabilities provided by the client. For example, get user information, local storage, location, etc.