Components & Templating
From “what a component is” to “how you write one”
Section titled “From “what a component is” to “how you write one””Foundations gave you the mental model: an .astro component is a server-rendered function of props that returns HTML. This module zooms into the template language itself — the exact syntax you use to turn data into markup, style it, sprinkle in browser behavior, and compose components together.
Nothing here hydrates or ships a framework. That is the next module (Islands). Here we stay on the server side of the line: the expressions, styles, slots, and Markdown that produce your HTML.
What this module covers
Section titled “What this module covers”| Lesson | What you’ll learn |
|---|---|
| Astro syntax deep dive | Expressions, conditionals, loops, Fragments, set:html, spread attributes |
| Styles & scripts | Scoped vs global CSS, define:vars, and how <script> really behaves |
| Slots & composition | Named slots, fallback content, checking slots, building layouts |
| Markdown & MDX | Markdown pages, applying layouts, and using components inside .mdx |
flowchart LR data["props and data"] --> tmpl["template: expressions, loops, slots"] css["scoped styles"] --> tmpl tmpl --> html["rendered HTML plus scoped CSS"] html --> ship["shipped to the browser, zero JS by default"]
The through-line
Section titled “The through-line”Every feature in this module is a way to describe HTML more expressively while keeping the zero-JS-by-default promise. Expressions and loops let data drive the markup. Scoped styles keep CSS local without ceremony. Slots let components wrap content they do not know in advance. And Markdown/MDX let you write content in prose while still dropping in components where you need them.