2018-11-24 16:44:53 +13:00
|
|
|
# Partials
|
|
|
|
|
|
|
|
Maud does not have a built-in concept of partials or sub-templates. Instead, you can compose your markup with any function that returns `Markup`.
|
|
|
|
|
2019-05-09 21:27:27 +12:00
|
|
|
The following example defines a `header` and `footer` function.
|
|
|
|
These functions are combined to form the final `page`.
|
2018-11-24 16:44:53 +13:00
|
|
|
|
|
|
|
```rust
|
2019-05-09 21:27:27 +12:00
|
|
|
use maud::{DOCTYPE, html, Markup};
|
2018-11-24 16:44:53 +13:00
|
|
|
|
|
|
|
/// A basic header with a dynamic `page_title`.
|
|
|
|
fn header(page_title: &str) -> Markup {
|
|
|
|
html! {
|
|
|
|
(DOCTYPE)
|
2019-05-09 21:27:27 +12:00
|
|
|
meta charset="utf-8";
|
|
|
|
title { (page_title) }
|
2018-11-24 16:44:53 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A static footer.
|
|
|
|
fn footer() -> Markup {
|
|
|
|
html! {
|
|
|
|
footer {
|
2019-05-09 21:27:27 +12:00
|
|
|
a href="rss.atom" { "RSS Feed" }
|
2018-11-24 16:44:53 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The final Markup, including `header` and `footer`.
|
|
|
|
///
|
|
|
|
/// Additionally takes a `greeting_box` that's `Markup`, not `&str`.
|
|
|
|
pub fn page(title: &str, greeting_box: Markup) -> Markup {
|
|
|
|
html! {
|
|
|
|
// Add the header markup to the page
|
|
|
|
(header(title))
|
2020-11-02 12:53:27 +13:00
|
|
|
h1 { (title) }
|
|
|
|
(greeting_box)
|
2018-11-24 16:44:53 +13:00
|
|
|
(footer())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-05-09 21:27:27 +12:00
|
|
|
Using the `page` function will return the markup for the whole page.
|
|
|
|
Here's an example:
|
2018-11-24 16:44:53 +13:00
|
|
|
|
|
|
|
```rust
|
2021-01-15 17:40:46 +13:00
|
|
|
# use maud::{html, Markup};
|
|
|
|
# fn page(title: &str, greeting_box: Markup) -> Markup { greeting_box }
|
2019-05-09 21:27:27 +12:00
|
|
|
page("Hello!", html! {
|
|
|
|
div { "Greetings, Maud." }
|
|
|
|
});
|
2018-11-24 16:44:53 +13:00
|
|
|
```
|