# Modules
FTS code can be written inside the <fts> tag in fxml files, or in files with the .fts extension.
# 1. Modules
Each .fts file and <fts> tag is an independent module.
Each module has its own independent scope. That is, variables and functions defined within a module are private by default and not visible to other modules.
To expose its internal private variables and functions to the outside, a module can only do so through module.exports.
# .fts Files
In the WeChat Developer Tools, you can right-click to create a .fts file directly and write FTS scripts inside it.
# Example Code
// /pages/comm.fts
var foo = "'hello world' from comm.fts";
var bar = function(d) {
return d;
}
module.exports = {
foo: foo,
bar: bar
};
The above example writes FTS code in the /pages/comm.fts file. This .fts file can be referenced by other .fts files or the <fts> tag in FXML.
# module Object
Each fts module has a built-in module object.
# Properties
exports: Through this property, private variables and functions of this module can be shared externally.
# Example Code
// /pages/tools.fts
var foo = "'hello world' from tools.fts";
var bar = function (d) {
return d;
}
module.exports = {
FOO: foo,
bar: bar,
};
module.exports.msg = "some msg";
<!-- page/index/index.fxml -->
<fts src="./../tools.fts" module="tools" />
<view> {{tools.msg}} </view>
<view> {{tools.bar(tools.FOO)}} </view>
Page Output:
some msg
'hello world' from tools.fts
# require Function
To reference other fts file modules within a .fts module, you can use the require function.
When referencing, note the following points:
- Only
.ftsfile modules can be referenced, and relative paths must be used. ftsmodules are singletons. When anftsmodule is referenced for the first time, it is automatically initialized as a singleton object. Multiple pages, multiple locations, and multiple references all use the sameftsmodule object.- If an
ftsmodule is defined but never referenced, it will not be parsed or executed.
# Example Code
// /pages/tools.fts
var foo = "'hello world' from tools.fts";
var bar = function (d) {
return d;
}
module.exports = {
FOO: foo,
bar: bar,
};
module.exports.msg = "some msg";
// /pages/logic.fts
var tools = require("./tools.wxs");
console.log(tools.FOO);
console.log(tools.bar("logic.wxs"));
console.log(tools.msg);
<!-- /page/index/index.fxml -->
<fts src="./../logic.fts" module="logic" />
Console Output:
'hello world' from tools.fts
logic.fts
some msg
# <fts> Tag
| Attribute Name | Type | Default Value | Description |
|---|---|---|---|
| module | String | The module name of the current <fts> tag. Required field. | |
| src | String | The relative path to the referenced .fts file. Only effective when this tag is a self-closing tag or its content is empty. |
# module Attribute
The module attribute is the module name of the current <fts> tag. Within a single FXML file, it is recommended that its value be unique. If there are duplicate module names, they will be overwritten in order (the latter overwrites the former). Module names of fts modules in different files do not overwrite each other.
The naming of the module attribute value must comply with the following two rules:
- The first character must be: a letter (a-zA-Z) or an underscore (_).
- The remaining characters can be: letters (a-zA-Z), underscores (_), or digits (0-9).
# Example Code
<!--fxml-->
<fts module="foo">
var some_msg = "hello world";
module.exports = {
msg : some_msg,
}
</fts>
<view> {{foo.msg}} </view>
Page Output:
hello world
The above example declares a module named foo, exposing the some_msg variable for use by the current page.
# src Attribute
The src attribute can be used to reference other fts file modules.
When referencing, note the following points:
- Only
.wxsfile modules can be referenced, and relative paths must be used. wxsmodules are singletons. When awxsmodule is referenced for the first time, it is automatically initialized as a singleton object. Multiple pages, multiple locations, and multiple references all use the samewxsmodule object.- If a
wxsmodule is defined but never referenced, it will not be parsed or executed.
# Example Code
// /pages/index/index.js
Page({
data: {
msg: "'hello wrold' from js",
}
})
<!-- /pages/index/index.fxml -->
<fts src="./../comm.fts" module="some_comms"></fts>
<!-- Alternatively, you can use the self-closing tag syntax:
<fts src="./../comm.fts" module="some_comms" />
-->
<!-- Call the bar function from the some_comms module with the parameter being foo from the some_comms module -->
<view> {{some_comms.bar(some_comms.foo)}} </view>
<!-- Call the bar function from the some_comms module with the parameter being msg from page/index/index.js -->
<view> {{some_comms.bar(msg)}} </view>
Page Output:
'hello world' from comm.wxs
'hello wrold' from js
The above example references the /page/comm.fts module via the <fts> tag in the file /page/index/index.fxml.
# 2. Notes
- The
<fts>module can only be accessed within the FXML file where it is defined. When using<include>or<import>, the<fts>module will not be imported into the corresponding FXML file. - Within the
<template>tag, only the<fts>modules defined in the FXML file that defines the<template>can be used.