The component template is the core part of the .gen
file, which is used to describe the page structure.
It is contained in the <template>
tag, and its syntax style is similar to HTML,
but it combines the enhanced syntax of the component system.
There are two ways to declare components: regular tags and self-close tags.
<$component_name></$component_name>
<$component_name />
$component_name
: component name$static_prop
: static property$prop_value
: property value$bind_prop
: binding property$bind_ident
: binding variable identifier$callback
: callback$callback_ident
: callback method identifier$arg
: parameter$()*
: many1, 1 or more$()?
: recognize, 0 or 1
In the syntax rules section, we will explain in detail the rules for writing component templates
The root component refers to the root component of the component template part in each .gen
file, that is, the first tag wrapped by the <template>
tag
In this example, the root component is <view id="UiRoot"></view>
Any component template has one and only one unique root component, and the root component must have a unique name
You may have discovered that the unique naming attributes used statically and dynamically are different. Static uses id
and dynamic uses name
The difference between them is that you cannot do any script-level code manipulation when using a static unique root, i.e. using <script>
This principle is only valid for the root component. For the root component only, the unique name is the component name when it is referenced externally. For example, when the unique name is Hello
, use <Hello></Hello>
in other components to represent the component
You can also use snake name rule
to name the root component, for example: my_view1
, but when it is introduced in other components, it will still be converted to camel name rule
, that is: <MyView1></MyView1>
The naming reference principle is only valid for external references
For the root component, the view
view component is inherited by default and cannot be changed. We hope that each encapsulated component can appear as an independent single view.
The above description of the root component id
and name
is called the internal principle and the external principle. id
is internal and name
is external, which means:
id
is only valid for the current encapsulated component. The external cannot obtain the internal component through the internal id
name
is only valid for the external, and is the way to use the encapsulated component externally. The component cannot be obtained by name
internally, and name
only exists in <component>
The writing method of static properties is: $static_prop="$prop_value"
, and multiple properties are separated by spaces
, for example:
The property value of the component is typed, wrong type cannot be compiled
For the type of property value, please refer to: Data Type
id
is the unique identifier of a component. Each component has this attribute. id
can be used to obtain the corresponding component. It is also the attribute identifier of the component
If you can't understand, please read [Inside and Outside Principles](#Inside and Outside Principles) first
class
is the attribute (style) identifier of a component. class
can be used to merge the style (<style>
) part into the component
class
is not uniqueclass
s, but cannot declare multiple class
sas_prop
property componentas_prop
property converts a component into a property of its parent component, which comes from a way to build slots in Makepad
Currently involved in button
, view
, popup
components
For properties that need to be bound, use :
as the property prefix, and the property value is of Bind::Ident
type, as follows:
Computed properties are values dynamically calculated based on existing properties. In GenUI, computed properties do not store state, but are automatically updated based on changes in their dependencies.
Features | Computed | Two-way Binding |
---|---|---|
Store state? | ❌ No | ✅ Yes |
Mutable? | ✅ Update when dependencies change | ✅ Yes |
Manually editable? | ✅ But cannot use set_xxx method | ✅ Modify via set_xxx |
Dependent data | ✅ Requires dependencies | ✅ Component state |
Computed properties are written by declaring them using methods when binding properties:
For a detailed description of computed properties, see Api: #[computed]
Callback means a method called after a component triggers an event, using @
as the callback prefix, as follows:
There may not be any parameters in the callback, but you still need to add ()
If the component uses a callback, you must add id
as an identifier
Since v0.1.2
, GenUI has introduced the automatic ID allocation strategy. During the component construction process, the system automatically checks whether each component has explicitly specified an id
. If not, a random unique ID will be generated for it.
This mechanism is extremely useful in many scenarios, including:
:prop="..."
)@event="..."
)The introduction of automatic ID greatly reduces the tediousness of manual identifier management and improves the simplicity and maintainability of template code.
Instead of using ulid
or other third-party libraries, we generate IDs according to the following custom strategy:
6 ~ 12
characters to avoid conflicts caused by being too short or redundant due to being too long.