mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
* 0.20 Changelog * Improve changelog generator * Add blog post * Add blog post * Apply suggestions from code review Co-authored-by: WorldSEnder <WorldSEnder@users.noreply.github.com> Co-authored-by: Julius Lungys <32368314+voidpumpkin@users.noreply.github.com> * update Changelog * update Cargo.toml * changelog gen compiles * website version 0.20 * add migration guides * prettier * i18n Co-authored-by: WorldSEnder <WorldSEnder@users.noreply.github.com> Co-authored-by: Julius Lungys <32368314+voidpumpkin@users.noreply.github.com>
61 lines
1.2 KiB
Plaintext
61 lines
1.2 KiB
Plaintext
---
|
|
title: Literals and Expressions
|
|
---
|
|
|
|
## リテラル
|
|
|
|
式が`Display`を実装した型を解決する場合、文字列に変換されて DOM に[Text](https://developer.mozilla.org/en-US/docs/Web/API/Text)ノードとして挿入されます。
|
|
|
|
テキストは式として処理されるため、全ての表示される内容は`{}`ブロックによって囲まれる必要があります。
|
|
これは Yew のアプリと通常の HTML の構文で最も異なる点です。
|
|
|
|
```rust
|
|
let text = "lorem ipsum";
|
|
html!{
|
|
<>
|
|
<div>{text}</div>
|
|
<div>{"dolor sit"}</div>
|
|
<span>{42}</span>
|
|
</>
|
|
}
|
|
```
|
|
|
|
## 式
|
|
|
|
HTML に`{}`ブロックを使って式を挿入することができます。
|
|
|
|
```rust
|
|
html! {
|
|
<div>
|
|
{
|
|
if show_link {
|
|
html! {
|
|
<a href="https://example.com">{"Link"}</a>
|
|
}
|
|
} else {
|
|
html! {}
|
|
}
|
|
}
|
|
</div>
|
|
}
|
|
```
|
|
|
|
式を関数やクロージャに分離するのはコードの可読性の観点から有効なことがあります。
|
|
|
|
```rust
|
|
let show_link = true;
|
|
let maybe_display_link = move || -> Html {
|
|
if show_link {
|
|
html! {
|
|
<a href="https://example.com">{"Link"}</a>
|
|
}
|
|
} else {
|
|
html! {}
|
|
}
|
|
};
|
|
|
|
html! {
|
|
<div>{maybe_display_link()}</div>
|
|
}
|
|
```
|