Start here · Lesson 3 of 22 · 10 min
Anatomy of an .astro file
The frontmatter fence, the template, and how expressions put data into HTML.
Every .astro file has two parts, split by a code fence of three dashes.
src/pages/index.astro
---
// 1. Component script: JavaScript or TypeScript
const businessName = 'Sunday Supper Co.';
const tagline = 'Seasonal home cooking for your table.';
const dishes = ['Charred corn', 'Pinto beans', 'Peach cobbler'];
---
<!-- 2. Template: HTML plus {expressions} -->
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>{businessName}</title>
</head>
<body>
<h1>{tagline}</h1>
<p>Welcome to {businessName}.</p>
<h2>On the menu</h2>
<ul>
{dishes.map((dish) => <li>{dish}</li>)}
</ul>
</body>
</html>
Replace everything in your index.astro with this and save.
The script runs at build time
The code between the fences runs on your computer when the site builds, not in the visitor’s browser. That means:
- You can import files, read data, and call APIs here.
- None of this code is sent to the browser.
- The visitor only gets the final HTML.
Curly braces put values into HTML
{businessName} prints the variable. You can use any JavaScript expression:
<p>{dishes.length} dishes tonight</p>
<p>{businessName.toUpperCase()}</p>
Loops use .map()
There’s no special loop syntax. Use .map() and return HTML:
{dishes.map((dish) => <li>{dish}</li>)}
Conditionals use && or ternaries
{dishes.length > 0 && <h2>On the menu</h2>}
{dishes.length > 0 ? <p>Open tonight</p> : <p>Closed tonight</p>}
Attributes take expressions too
---
const menuUrl = '/menu';
---
<a href={menuUrl}>See the menu</a>