📖 Scenario: You are building a simple Astro project to understand why Astro components use a unique format combining HTML, JavaScript, and frontmatter.
🎯 Goal: Create a basic Astro component that shows how frontmatter and HTML work together in Astro's unique format.
📋 What You'll Learn
Create an Astro component file with frontmatter section
Add a JavaScript variable inside the frontmatter
Use the variable inside the HTML part of the component
Understand the separation of frontmatter and template in Astro components
💡 Why This Matters
🌍 Real World
Astro components are used to build fast, modern websites by combining HTML and JavaScript cleanly.
💼 Career
Understanding Astro's unique component format helps developers create maintainable and efficient web projects.
Progress0 / 4 steps
1
Create frontmatter section with a variable
Create an Astro component file and add a frontmatter section at the top with a variable called message set to 'Hello from Astro!'.
Astro
Hint
The frontmatter section in Astro is enclosed by triple dashes --- at the top of the file.
2
Add HTML template using the variable
Below the frontmatter, add an <h1> tag that displays the message variable using curly braces {message}.
Astro
Hint
Use curly braces {} to insert JavaScript variables inside the HTML template in Astro.
3
Explain the separation of frontmatter and template
Add a comment inside the frontmatter explaining that this section is for JavaScript logic, and a comment above the <h1> tag explaining it is the HTML template.
Astro
Hint
Use JavaScript comments // inside frontmatter and HTML comments <!-- --> in the template.
4
Add a second variable and use it in the template
Inside the frontmatter, add a new variable called subtitle set to 'Astro combines HTML and JS'. Then add a <h2> tag below the <h1> that displays {subtitle}.
Astro
Hint
Declare the new variable inside frontmatter and use curly braces to insert it in the template.
Practice
(1/5)
1. Why do Astro components use a unique format that combines JavaScript, HTML, and CSS in one file?
easy
A. To prevent using HTML inside components
B. To make building fast websites easier by loading less code
C. To separate styles into different files only
D. To force developers to write only JavaScript
Solution
Step 1: Understand Astro's design goal
Astro aims to build fast websites by reducing unnecessary code sent to browsers.
Step 2: Recognize the benefit of combining code
Combining JavaScript, HTML, and CSS in one file helps Astro optimize loading and keep components simple.
Final Answer:
To make building fast websites easier by loading less code -> Option B
Quick Check:
Unique format = faster websites [OK]
Hint: Unique format helps load less code for speed [OK]
Common Mistakes:
Thinking Astro forbids HTML in components
Believing styles must be in separate files
Assuming only JavaScript is allowed
2. Which of the following is the correct way to start an Astro component file?
easy
A. ---
const name = 'Astro';
---
B. function Astro() { return <div>Hello</div>; }
C. <script>const name = 'Astro';</script>
D. import React from 'react';
Solution
Step 1: Identify Astro component syntax
Astro components start with a frontmatter block enclosed by triple dashes --- for JavaScript.
Step 2: Compare options
---
const name = 'Astro';
--- uses the correct frontmatter syntax; others use React or HTML script tags not used in Astro.
Final Answer:
---\nconst name = 'Astro';\n--- -> Option A
Quick Check:
Astro frontmatter uses --- [OK]
Hint: Astro JS code is inside --- frontmatter [OK]