2020-11-02 12:53:27 +13:00
|
|
|
|
# Elements and attributes
|
|
|
|
|
|
2020-12-01 10:44:05 +13:00
|
|
|
|
## Elements with contents: `p {}`
|
2020-11-02 12:53:27 +13:00
|
|
|
|
|
2020-12-01 10:44:05 +13:00
|
|
|
|
Write an element using curly braces:
|
2020-11-02 12:53:27 +13:00
|
|
|
|
|
|
|
|
|
```rust
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# let _ = maud::
|
2020-11-02 12:53:27 +13:00
|
|
|
|
html! {
|
|
|
|
|
h1 { "Poem" }
|
2020-12-01 10:44:05 +13:00
|
|
|
|
p {
|
|
|
|
|
strong { "Rock," }
|
|
|
|
|
" you are a rock."
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# ;
|
2020-12-01 10:44:05 +13:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Void elements: `br;`
|
|
|
|
|
|
|
|
|
|
Terminate a void element using a semicolon:
|
|
|
|
|
|
|
|
|
|
```rust
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# let _ = maud::
|
2020-12-01 10:44:05 +13:00
|
|
|
|
html! {
|
|
|
|
|
link rel="stylesheet" href="poetry.css";
|
2020-11-02 12:53:27 +13:00
|
|
|
|
p {
|
|
|
|
|
"Rock, you are a rock."
|
|
|
|
|
br;
|
|
|
|
|
"Gray, you are gray,"
|
|
|
|
|
br;
|
|
|
|
|
"Like a rock, which you are."
|
|
|
|
|
br;
|
|
|
|
|
"Rock."
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# ;
|
2020-11-02 12:53:27 +13:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-22 19:31:51 +13:00
|
|
|
|
The result will be rendered with HTML syntax –
|
|
|
|
|
`<br>` not `<br />`.
|
2020-12-01 10:44:05 +13:00
|
|
|
|
|
|
|
|
|
## Custom elements and `data` attributes
|
2020-11-02 12:53:27 +13:00
|
|
|
|
|
2021-01-22 19:31:51 +13:00
|
|
|
|
Maud also supports elements and attributes with hyphens in them.
|
|
|
|
|
This includes [custom elements], [data attributes], and [ARIA annotations].
|
2020-11-02 12:53:27 +13:00
|
|
|
|
|
|
|
|
|
```rust
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# let _ = maud::
|
2020-11-02 12:53:27 +13:00
|
|
|
|
html! {
|
2020-12-01 10:44:05 +13:00
|
|
|
|
article data-index="12345" {
|
|
|
|
|
h1 { "My blog" }
|
|
|
|
|
tag-cloud { "pinkie pie pony cute" }
|
2020-11-02 12:53:27 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# ;
|
2020-11-02 12:53:27 +13:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
[custom elements]: https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements
|
2020-12-01 10:44:05 +13:00
|
|
|
|
[data attributes]: https://css-tricks.com/a-complete-guide-to-data-attributes/
|
|
|
|
|
[ARIA annotations]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Annotations
|
2020-11-02 12:53:27 +13:00
|
|
|
|
|
|
|
|
|
## Non-empty attributes: `title="yay"`
|
|
|
|
|
|
2021-01-22 19:31:51 +13:00
|
|
|
|
Add attributes using the syntax:
|
|
|
|
|
`attr="value"`.
|
|
|
|
|
You can attach any number of attributes to an element.
|
|
|
|
|
The values must be quoted:
|
|
|
|
|
they are parsed as string literals.
|
2020-11-02 12:53:27 +13:00
|
|
|
|
|
|
|
|
|
```rust
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# let _ = maud::
|
2020-11-02 12:53:27 +13:00
|
|
|
|
html! {
|
|
|
|
|
ul {
|
|
|
|
|
li {
|
|
|
|
|
a href="about:blank" { "Apple Bloom" }
|
|
|
|
|
}
|
|
|
|
|
li class="lower-middle" {
|
|
|
|
|
"Sweetie Belle"
|
|
|
|
|
}
|
|
|
|
|
li dir="rtl" {
|
|
|
|
|
"Scootaloo "
|
|
|
|
|
small { "(also a chicken)" }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# ;
|
2020-11-02 12:53:27 +13:00
|
|
|
|
```
|
|
|
|
|
|
2021-10-29 07:24:34 +02:00
|
|
|
|
## Optional attributes: `title=[Some("value")]`
|
|
|
|
|
|
2021-11-29 22:06:56 +11:00
|
|
|
|
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`.
|
2021-10-29 07:24:34 +02:00
|
|
|
|
|
|
|
|
|
```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" }
|
|
|
|
|
}
|
|
|
|
|
# ;
|
|
|
|
|
```
|
|
|
|
|
|
2020-11-11 16:47:23 +13:00
|
|
|
|
## Empty attributes: `checked`
|
2020-11-02 12:53:27 +13:00
|
|
|
|
|
2020-11-11 16:47:23 +13:00
|
|
|
|
Declare an empty attribute by omitting the value.
|
2020-11-02 12:53:27 +13:00
|
|
|
|
|
|
|
|
|
```rust
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# let _ = maud::
|
2020-11-02 12:53:27 +13:00
|
|
|
|
html! {
|
|
|
|
|
form {
|
2020-11-11 16:47:23 +13:00
|
|
|
|
input type="checkbox" name="cupcakes" checked;
|
2020-11-02 12:53:27 +13:00
|
|
|
|
" "
|
|
|
|
|
label for="cupcakes" { "Do you like cupcakes?" }
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# ;
|
2020-11-02 12:53:27 +13:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-22 19:31:51 +13:00
|
|
|
|
Before version 0.22.2,
|
|
|
|
|
Maud required a `?` suffix on empty attributes:
|
|
|
|
|
`checked?`.
|
|
|
|
|
This is no longer necessary ([#238]),
|
|
|
|
|
but still supported for backward compatibility.
|
2020-11-11 16:47:23 +13:00
|
|
|
|
|
|
|
|
|
[#238]: https://github.com/lambda-fairy/maud/pull/238
|
|
|
|
|
|
2020-11-02 12:53:27 +13:00
|
|
|
|
## Classes and IDs: `.foo` `#bar`
|
|
|
|
|
|
2021-01-22 19:31:51 +13:00
|
|
|
|
Add classes and IDs to an element
|
|
|
|
|
using `.foo` and `#bar` syntax.
|
|
|
|
|
You can chain multiple classes and IDs together,
|
|
|
|
|
and mix and match them with other attributes:
|
2020-11-02 12:53:27 +13:00
|
|
|
|
|
|
|
|
|
```rust
|
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
|
|
|
|
input #cannon .big.scary.bright-red type="button" value="Launch Party Cannon";
|
2021-11-01 17:18:48 +11:00
|
|
|
|
}
|
|
|
|
|
# ;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
In Rust 2021,
|
|
|
|
|
the `#` symbol must be preceded by a space,
|
|
|
|
|
to avoid conflicts with [reserved syntax]:
|
|
|
|
|
|
|
|
|
|
[reserved syntax]: https://doc.rust-lang.org/edition-guide/rust-2021/reserving-syntax.html
|
|
|
|
|
|
|
|
|
|
```rust,edition2018
|
|
|
|
|
# let _ = maud::
|
|
|
|
|
html! {
|
|
|
|
|
// Works on all Rust editions
|
|
|
|
|
input #pinkie;
|
|
|
|
|
|
|
|
|
|
// Works on Rust 2018 and older only
|
|
|
|
|
input#pinkie;
|
2020-11-02 12:53:27 +13:00
|
|
|
|
}
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# ;
|
2020-11-02 12:53:27 +13:00
|
|
|
|
```
|
|
|
|
|
|
2021-01-22 19:31:51 +13:00
|
|
|
|
The classes and IDs can be quoted.
|
|
|
|
|
This is useful for names with numbers or symbols
|
|
|
|
|
which otherwise wouldn't parse:
|
2020-12-01 10:44:05 +13:00
|
|
|
|
|
|
|
|
|
```rust
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# let _ = maud::
|
2020-12-01 10:44:05 +13:00
|
|
|
|
html! {
|
2021-11-06 21:51:34 +11:00
|
|
|
|
div."col-sm-2" { "Bootstrap column!" }
|
2020-12-01 10:44:05 +13:00
|
|
|
|
}
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# ;
|
2020-12-01 10:44:05 +13:00
|
|
|
|
```
|
|
|
|
|
|
2020-11-02 12:53:27 +13:00
|
|
|
|
## Implicit `div` elements
|
|
|
|
|
|
2021-01-22 19:31:51 +13:00
|
|
|
|
If the element name is omitted,
|
|
|
|
|
but there is a class or ID,
|
|
|
|
|
then it is assumed to be a `div`.
|
2020-11-02 12:53:27 +13:00
|
|
|
|
|
|
|
|
|
```rust
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# let _ = maud::
|
2020-11-02 12:53:27 +13:00
|
|
|
|
html! {
|
|
|
|
|
#main {
|
|
|
|
|
"Main content!"
|
2020-12-01 10:44:05 +13:00
|
|
|
|
.tip { "Storing food in a refrigerator can make it 20% cooler." }
|
2020-11-02 12:53:27 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-15 17:40:46 +13:00
|
|
|
|
# ;
|
2020-11-02 12:53:27 +13:00
|
|
|
|
```
|