2020-11-02 12:53:27 +13:00
# Splices and toggles
2018-11-24 16:44:53 +13:00
2020-11-02 12:53:27 +13:00
## Splices: `(foo)`
2021-01-22 19:31:51 +13:00
Use `(foo)` syntax to insert the value of `foo` at runtime.
Any HTML special characters are escaped by default.
2018-11-24 16:44:53 +13:00
```rust
let best_pony = "Pinkie Pie";
let numbers = [1, 2, 3, 4];
2021-01-15 17:40:46 +13:00
# let _ = maud::
2018-11-24 16:44:53 +13:00
html! {
p { "Hi, " (best_pony) "!" }
p {
"I have " (numbers.len()) " numbers, "
"and the first one is " (numbers[0])
}
}
2021-01-15 17:40:46 +13:00
# ;
2018-11-24 16:44:53 +13:00
```
2021-01-22 19:31:51 +13:00
Arbitrary Rust code can be included in a splice by using a [block].
2023-02-12 22:19:56 +11:00
This can be helpful for complex expressions that would be difficult to read otherwise.
2018-11-24 16:44:53 +13:00
```rust
2021-01-15 17:40:46 +13:00
# struct Foo;
# impl Foo { fn time(self) -> Bar { Bar } }
# struct Bar;
# impl Bar { fn format(self, _: &str) -> &str { "" } }
# fn something_convertible_to_foo() -> Option<Foo> { Some(Foo) }
# fn test() -> Option<()> {
# let _ = maud::
2018-11-24 16:44:53 +13:00
html! {
p {
({
let f: Foo = something_convertible_to_foo()?;
f.time().format("%H%Mh")
})
}
}
2021-01-15 17:40:46 +13:00
# ;
# Some(())
# }
2018-11-24 16:44:53 +13:00
```
2021-01-22 19:31:51 +13:00
[block]: https://doc.rust-lang.org/reference.html#block -expressions
2020-11-02 12:53:27 +13:00
### Splices in attributes
2018-11-24 16:44:53 +13:00
Splices work in attributes as well.
```rust
let secret_message = "Surprise!";
2021-01-15 17:40:46 +13:00
# let _ = maud::
2018-11-24 16:44:53 +13:00
html! {
p title=(secret_message) {
"Nothing to see here, move along."
}
}
2021-01-15 17:40:46 +13:00
# ;
2018-11-24 16:44:53 +13:00
```
2023-02-12 22:19:56 +11:00
To concatenate multiple values within an attribute, wrap the whole thing in braces.
2021-01-22 19:31:51 +13:00
This syntax is useful for building URLs.
2018-11-24 16:44:53 +13:00
```rust
const GITHUB: & 'static str = "https://github.com";
2021-01-15 17:40:46 +13:00
# let _ = maud::
2018-11-24 16:44:53 +13:00
html! {
2019-09-14 13:48:59 +12:00
a href={ (GITHUB) "/lambda-fairy/maud" } {
2018-11-24 16:44:53 +13:00
"Fork me on GitHub"
}
}
2021-01-15 17:40:46 +13:00
# ;
2018-11-24 16:44:53 +13:00
```
2020-11-02 16:33:03 +11:00
### Splices in classes and IDs
Splices can also be used in classes and IDs.
```rust
let name = "rarity";
let severity = "critical";
2021-01-15 17:40:46 +13:00
# let _ = maud::
2020-11-02 16:33:03 +11:00
html! {
2021-11-01 17:18:48 +11:00
aside #(name) {
2021-11-06 21:51:34 +11:00
p.{ "color-" (severity) } { "This is the worst! Possible! Thing!" }
2020-11-02 16:33:03 +11:00
}
}
2021-01-15 17:40:46 +13:00
# ;
2020-11-02 16:33:03 +11:00
```
2020-11-02 12:53:27 +13:00
### What can be spliced?
2018-11-24 16:44:53 +13:00
2021-11-21 19:58:58 +11:00
You can splice any value that implements [`Render` ][Render].
2023-02-12 22:19:56 +11:00
Most primitive types (such as `str` and `i32` ) implement this trait, so they should work out of the box.
To get this behavior for a custom type, you can implement the [`Render` ][Render] trait by hand.
The [`PreEscaped` ][PreEscaped] wrapper type, which outputs its argument without escaping, works this way.
2021-01-22 19:31:51 +13:00
See the [traits ](render-trait.md ) section for details.
2018-11-24 16:44:53 +13:00
```rust
use maud::PreEscaped;
let post = "< p > Pre-escaped< / p > ";
2021-01-15 17:40:46 +13:00
# let _ = maud::
2018-11-24 16:44:53 +13:00
html! {
h1 { "My super duper blog post" }
(PreEscaped(post))
}
2021-01-15 17:40:46 +13:00
# ;
2018-11-24 16:44:53 +13:00
```
[Render]: https://docs.rs/maud/*/maud/trait.Render.html
[PreEscaped]: https://docs.rs/maud/*/maud/struct.PreEscaped.html
2020-11-02 12:53:27 +13:00
## Toggles: `[foo]`
2024-06-16 13:15:27 +02:00
Use `[foo]` syntax to show or hide classes and [boolean attributes ](https://developer.mozilla.org/en-US/docs/Glossary/Boolean/HTML ) on a HTML element based on a boolean expression `foo` .
2020-11-02 12:53:27 +13:00
2024-06-16 13:15:27 +02:00
Toggle boolean attributes:
2020-11-02 12:53:27 +13:00
```rust
let allow_editing = true;
2021-01-15 17:40:46 +13:00
# let _ = maud::
2020-11-02 12:53:27 +13:00
html! {
2020-11-11 16:47:23 +13:00
p contenteditable[allow_editing] {
2020-11-02 12:53:27 +13:00
"Edit me, I "
em { "dare" }
" you."
}
}
2021-01-15 17:40:46 +13:00
# ;
2020-11-02 12:53:27 +13:00
```
And classes:
```rust
let cuteness = 95;
2021-01-15 17:40:46 +13:00
# let _ = maud::
2020-11-02 12:53:27 +13:00
html! {
2021-11-06 21:51:34 +11:00
p.cute[cuteness > 50] { "Squee!" }
2020-11-02 12:53:27 +13:00
}
2021-01-15 17:40:46 +13:00
# ;
2020-11-02 12:53:27 +13:00
```
2023-02-13 12:34:34 +11:00
### Optional attributes with values: `title=[Some("value")]`
Add optional attributes to an element using `attr=[value]` syntax, with *square* brackets.
These are only rendered if the value is `Some<T>` , and entirely omitted if the value is `None` .
```rust
# let _ = maud::
html! {
p title=[Some("Good password")] { "Correct horse" }
@let value = Some(42);
input value=[value];
@let title: Option<& str> = None;
p title=[title] { "Battery staple" }
}
# ;
2024-06-16 13:15:27 +02:00
```