maomi
介绍
入门
文档

模板分支

if 语句

可以使用 if 语句。

#[component(Backend = DomBackend)]
struct MyWebsite {
template: template! {
if self.my_boolean_data {
<div> "My Optional Element" </div>
} else {
<div> "Another Element" </div>
}
},
my_boolean_data: bool,
}

类似地,也可以用 if-let 语句。

#[component(Backend = DomBackend)]
struct MyWebsite {
template: template! {
if let Some(s) = &self.my_option {
<div> { s } </div>
}
},
my_option: Option<String>,
}

match 语句

match 语句的用法就像普通 rust 代码一样。

#[component(Backend = DomBackend)]
struct MyWebsite {
template: template! {
match &self.my_option {
Some(s) => {
<div> { s } </div>
}
None => {
<div> "Another Element" </div>
}
}
},
my_option: Option<String>,
}