The page! Macro

The page! macro is the entry point for rendering a full HTML page. Use it inside route handlers to produce a Page struct that your framework serves directly.

Basic Usage

page! accepts the same template syntax as the view! macro: HTML tags, Rust expressions, and control-flow blocks. It creates and returns a Page struct.

1use tidos::{page, Page};
2use rocket::{get, routes};
3
4#[get("/")]
5pub fn index() -> Page {
6 page! {
7 <main>
8 <h1>{"Hello, world!"}</h1>
9 <p>{"Built with Tidos."}</p>
10 </main>
11 }
12}
13
14#[rocket::main]
15async fn main() {
16 rocket::build()
17 .mount("/", routes![index])
18 .launch()
19 .await
20 .unwrap();
21}

Using Components

Any struct implementing the Component trait can be used in your pages.

1use tidos::{page, view, Component, Page};
2use rocket::get;
3
4pub struct Greeting {
5 pub name: String,
6}
7
8impl Component for Greeting {
9 fn to_render(&self, page: &mut Page) {
10 view! {
11 <h1>{"Hello, {}!", &self.name}</h1>
12 }
13 }
14}
15
16#[get("/")]
17pub fn index() -> Page {
18 page! {
19 <main>
20 <Greeting name={"World".to_string()} />
21 </main>
22 }
23}