Skip to content

Commit 7158acd

Browse files
committed
if_else sugar
1 parent 786bc91 commit 7158acd

File tree

4 files changed

+132
-14
lines changed

4 files changed

+132
-14
lines changed

docs/v0.1.0/en/doc/tutorial/template/basic.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,24 @@ Callback means a method called after a component triggers an event, using `@` as
210210
> There may not be any parameters in the callback, but you still need to add `()`
211211
>
212212
> If the component uses a callback, you must add `id` as an identifier
213+
214+
### Automatic ID strategy
215+
216+
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.
217+
218+
This mechanism is extremely useful in many scenarios, including:
219+
220+
- Property binding (`:prop="..."`)
221+
- Event callback (`@event="..."`)
222+
- Computed property association
223+
- Various syntax sugar processing
224+
225+
The introduction of automatic ID greatly reduces the tediousness of manual identifier management and improves the simplicity and maintainability of template code.
226+
227+
#### ID generation rules
228+
229+
Instead of using `ulid` or other third-party libraries, we generate IDs according to the following custom strategy:
230+
231+
1. **Start with an English letter**: Ensure that the variable naming specification is met to avoid potential syntax conflicts.
232+
2. **Length limit**: The ID length is controlled between `6 ~ 12` characters to avoid conflicts caused by being too short or redundant due to being too long.
233+
3. **Character composition**: only contain **English letters and numbers**, no special characters, to maintain compatibility and readability.

docs/v0.1.0/en/doc/tutorial/template/sugar_syntax.mdx

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ The followings are details:
2828
2929
### Examples
3030

31-
> [!DANGER]
32-
>
33-
> When using syntax sugar, you need to bind the id just like the bind type
34-
>
35-
> We plan to introduce an automatic id strategy in the `v0.1.3` version (automatically generate an id for each component that does not have an id added during static compilation)
36-
3731
#### Single for
3832

3933
```html
@@ -56,4 +50,48 @@ The followings are details:
5650
</view>
5751
```
5852

59-
## `If_ElseIf_Else` <Badge text="Comming Soon" type="warning" />
53+
## `If_ElseIf_Else`
54+
55+
The `if_else` syntax sugar allows users to conditionally render components. The component will only be rendered if the expression returns `true`.
56+
57+
### Syntax
58+
59+
```html
60+
<label :if="$if_bind_condition"></label>
61+
$($(<label :else_if="$else_if_bind_condition"></label>)*)?
62+
$(<label else></label>)?
63+
```
64+
65+
> - `$if_bind_condition`: if conditional statement condition
66+
> - `$else_if_bind_condition`: else_if conditional statement condition
67+
> - `$()*`: many1, 1 or more
68+
> - `$()?`: recognize, 0 or 1
69+
70+
> [!NOTE]
71+
>
72+
> Among them, `$($(<label :else_if="$else_if_bind_condition"></label>)*)?` means that `else_if` can be present or absent, and if present, it can be multiple
73+
74+
### Example
75+
76+
For specific writing and examples, see: [if_sugar tests](https://github.com/Privoce/made_with_GenUI/tree/main/tests/if_sugar/views)
77+
78+
#### Only if
79+
80+
```html
81+
<label :if="vis" text="'hello'"></label>
82+
```
83+
84+
#### if else
85+
86+
```html
87+
<label :if="vis" text="'hello'"></label>
88+
<label else text="'world'"></label>
89+
```
90+
91+
#### if else_if else
92+
93+
```html
94+
<label :if="is_a()" text="'Vis A'"></label>
95+
<label :else_if="is_b()" text="'Vis B'"></label>
96+
<label else text="'world'"></label>
97+
```

docs/v0.1.0/zh/doc/tutorial/template/basic.mdx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,24 @@ import { Tab, Tabs } from "rspress/theme";
205205
> 回调中可能没有任何参数,但依然需要添加`()`
206206
>
207207
> 若组件使用了回调一定要添加`id`作为标识符
208+
209+
### 自动 ID 策略
210+
211+
`v0.1.2` 起,GenUI 引入了**自动 ID 分配策略**。在组件构建过程中,系统会自动检查每个组件是否已显式指定 `id`,若未指定,则会为其生成一个随机的唯一 ID。
212+
213+
这一机制在多个场景下都极为有用,包括:
214+
215+
- 属性绑定(`:prop="..."`
216+
- 事件回调(`@event="..."`
217+
- 计算属性关联
218+
- 各类语法糖处理
219+
220+
自动 ID 的引入极大地减少了手动管理标识符的繁琐,提高了模板代码的简洁性和可维护性。
221+
222+
#### ID 生成规则
223+
224+
我们并未采用 `ulid` 或其他第三方库,而是根据以下自定义策略生成 ID:
225+
226+
1. **以英文字母开头**:确保符合变量命名规范,避免潜在语法冲突。
227+
2. **长度限制**:ID 长度控制在 `6 ~ 12` 个字符之间,避免过短冲突或过长冗余。
228+
3. **字符组成**:仅包含**英文字母与数字**,不含特殊字符,以保持兼容性和可读性。

docs/v0.1.0/zh/doc/tutorial/template/sugar_syntax.mdx

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ import { Badge } from '@theme';
2727
2828
### 例子
2929

30-
> [!DANGER]
31-
>
32-
> 在使用语法糖时和bind类型一样都需要进行id绑定
33-
>
34-
> 我们计划在`v0.1.3`版本引入自动id策略 (在静态编译时为每个没有添加id的组件自动生成id)
35-
3630
#### 单个
3731

3832
```html
@@ -55,4 +49,48 @@ import { Badge } from '@theme';
5549
</view>
5650
```
5751

58-
## `If_ElseIf_Else` <Badge text="Comming Soon" type="warning" />
52+
## `If_ElseIf_Else`
53+
54+
`if_else`语法糖允许使用者条件性地渲染组件。该组件只会在表达式返回`true`时才被渲染。
55+
56+
### 语法
57+
58+
```html
59+
<label :if="$if_bind_condition"></label>
60+
$($(<label :else_if="$else_if_bind_condition"></label>)*)?
61+
$(<label else></label>)?
62+
```
63+
64+
> - `$if_bind_condition`: if条件语句条件
65+
> - `$else_if_bind_condition`: else_if条件语句条件
66+
> - `$()*`: many1,1 个或多个
67+
> - `$()?`: recognize,0 个或 1 个
68+
69+
> [!NOTE]
70+
>
71+
> 其中`$($(<label :else_if="$else_if_bind_condition"></label>)*)?`表示`else_if`可以有也可以没有,如果有也可以有多个
72+
73+
### 例子
74+
75+
具体写法与示例见: [if_sugar tests](https://github.com/Privoce/made_with_GenUI/tree/main/tests/if_sugar/views)
76+
77+
#### 单个if
78+
79+
```html
80+
<label :if="vis" text="'hello'"></label>
81+
```
82+
83+
#### if else
84+
85+
```html
86+
<label :if="vis" text="'hello'"></label>
87+
<label else text="'world'"></label>
88+
```
89+
90+
#### if else_if else
91+
92+
```html
93+
<label :if="is_a()" text="'Vis A'"></label>
94+
<label :else_if="is_b()" text="'Vis B'"></label>
95+
<label else text="'world'"></label>
96+
```

0 commit comments

Comments
 (0)