--- name: maud-syntax-fundamentals description: Compile-time HTML templating with Maud using the html! macro for type-safe markup generation. Covers syntax patterns including elements, attributes, classes, IDs, content splicing, toggles, control flow (if/match/for), and DOCTYPE. Use when generating HTML in Rust, creating templates, or building server-side rendered pages. --- # Maud Syntax Fundamentals *Compile-time HTML templating for Rust with type safety and zero runtime overhead* ## Version Context - **Maud**: 0.27.0 (latest stable) - **Rust Edition**: 2021 - **Runtime**: ~100 SLoC (minimal overhead) ## When to Use This Skill - Generating HTML from Rust code - Building server-side rendered templates - Creating type-safe HTML components - Working with the `html!` macro - Replacing runtime templating engines (Tera, Handlebars) - Email template generation - RSS/Atom feed generation ## Core Philosophy 1. **Compile-time validation** - Template errors caught by rustc, not at runtime 2. **Type safety** - Leverages Rust's type system for HTML generation 3. **Zero overhead** - Templates compile to optimized Rust code 4. **Auto-escaping** - All text content is HTML-escaped by default 5. **No external dependencies** - Everything links into your binary ## Syntax Formula ``` ELEMENT[.CLASS][#ID][ ATTRIBUTES] { CONTENT } ``` **Key Symbols:** - `{}` = Container elements with content - `;` = Void/self-closing elements - `()` = Runtime value splicing (escaped) - `[]` = Conditional attributes/classes (toggles) - `@` = Control flow (if, match, for, while, let) ## Elements ### Container Elements ```rust use maud::html; html! { h1 { "Hello, world!" } p { "Paragraph text" } div { span { "Nested content" } } article { h2 { "Title" } p { "Content goes here" } } } ``` ### Void Elements (Self-Closing) ```rust html! { br; hr; input type="text" name="email"; img src="photo.jpg" alt="Description"; link rel="stylesheet" href="styles.css"; meta charset="UTF-8"; } ``` **Important**: Terminate with `;` - renders as `
` not `
` ### DOCTYPE Declaration ```rust use maud::{html, DOCTYPE}; html! { (DOCTYPE) html lang="en" { head { meta charset="UTF-8"; title { "My Page" } } body { h1 { "Content" } } } } ``` ## Classes and IDs ### Classes (Chainable) ```rust html! { div.container { } div.row.justify-center { } p.text-lg.font-bold { "Styled text" } // Quoted for special characters (hyphens, numbers) div."col-sm-2" { } div."bg-blue-500" { } } ``` ### IDs (Space Required in Rust 2021+) ```rust html! { div #main { } // ✅ Rust 2021+ section #content { } article #"post-123" { } // Quoted for hyphens/numbers } // ❌ Error in Rust 2021+ // div#main { } // Missing space before # ``` ### Implicit Divs ```rust html! { #header { } //