Component structure (frontmatter + template) in Astro
📖 Scenario: You are building a simple Astro component to display a greeting message on a webpage. Astro components have two main parts: the frontmatter where you write JavaScript code, and the template where you write HTML that uses that code.
🎯 Goal: Create an Astro component with a frontmatter section that defines a variable, and a template section that displays that variable inside an <h1> tag.
📋 What You'll Learn
Create a frontmatter section using --- to define a variable called greeting with the value 'Hello, Astro!'
Create a template section that uses the {greeting} variable inside an <h1> tag
💡 Why This Matters
🌍 Real World
Astro components are used to build fast, modern websites by combining JavaScript logic and HTML templates cleanly.
💼 Career
Understanding Astro component structure is essential for front-end developers working with Astro to create reusable UI parts.
Progress0 / 4 steps
1
Create the frontmatter section with a greeting variable
Create the frontmatter section using --- at the top of the file. Inside it, define a variable called greeting and set it to the string 'Hello, Astro!'.
Astro
Hint
Remember, the frontmatter in Astro is wrapped between --- lines.
2
Add the template section with an <h1> tag
Below the frontmatter, add an <h1> tag that displays the {greeting} variable.
Astro
Hint
Use curly braces {} to insert JavaScript variables inside the template.
3
Ensure the frontmatter and template are correctly separated
Make sure the frontmatter section is closed with --- before starting the template section with the <h1> tag.
Astro
Hint
The frontmatter must be at the top and enclosed by ---. The template comes after.
4
Complete the Astro component structure
Confirm the Astro component has a frontmatter section defining greeting and a template section displaying it inside an <h1> tag, with no extra code.
Astro
Hint
This is the complete minimal Astro component structure for this task.
Practice
(1/5)
1. What is the purpose of the frontmatter section in an Astro component?
easy
A. To style the component with CSS
B. To write JavaScript code and define variables used in the template
C. To write HTML markup for the component
D. To import other components only
Solution
Step 1: Understand frontmatter role
The frontmatter section is where you write JavaScript code, such as variables or functions, that the template can use.
Step 2: Differentiate from template
The template part is for HTML markup, not code. Styling is done separately, not in frontmatter.
Final Answer:
To write JavaScript code and define variables used in the template -> Option B
Quick Check:
Frontmatter = JavaScript code [OK]
Hint: Frontmatter holds code; template holds HTML display [OK]
Common Mistakes:
Confusing frontmatter with template HTML
Thinking frontmatter is for CSS styling
Believing frontmatter is only for imports
2. Which of the following is the correct way to start the frontmatter section in an Astro component?
easy
A. //
B. { }
C. /* */
D. ---
Solution
Step 1: Identify frontmatter syntax
Astro uses triple dashes --- at the top and bottom to mark the frontmatter section.
Step 2: Compare options
Curly braces, comments, or single slashes are not used to mark frontmatter.
Final Answer:
--- -> Option D
Quick Check:
Frontmatter starts with --- [OK]
Hint: Frontmatter always starts and ends with --- [OK]
Common Mistakes:
Using curly braces to start frontmatter
Confusing comments with frontmatter markers
Omitting the closing ---
3. Given this Astro component code, what will be displayed when rendered?
---
const greeting = "Hello, Astro!";
---
{greeting}
medium
A.
{greeting}
B.
C.
Hello, Astro!
D. Error: greeting is undefined
Solution
Step 1: Understand variable usage
The variable greeting is defined in frontmatter with value "Hello, Astro!".
Step 2: Template interpolation
The template uses curly braces {greeting} to insert the variable's value into the HTML.
Final Answer:
<h1>Hello, Astro!</h1> -> Option C
Quick Check:
Curly braces insert variable value [OK]
Hint: Curly braces show frontmatter variables in template [OK]
Common Mistakes:
Thinking curly braces show as text literally
Assuming variable is undefined without frontmatter
Forgetting to close frontmatter with ---
4. What is wrong with this Astro component code?
---
const name = "Astro";
Hello, {name}!
---
medium
A. The template HTML is inside the frontmatter section
B. The variable name is not defined
C. Curly braces cannot be used in Astro templates
D. Missing closing tag for <h1>
Solution
Step 1: Identify frontmatter boundaries
Frontmatter is only for JavaScript code and must be enclosed between --- lines.
Step 2: Check template placement
The HTML template must be outside the frontmatter. Here, the <h1> is inside frontmatter, causing an error.
Final Answer:
The template HTML is inside the frontmatter section -> Option A
Quick Check:
Template must be outside frontmatter [OK]
Hint: Keep HTML outside --- frontmatter --- [OK]
Common Mistakes:
Placing HTML inside frontmatter
Forgetting to close frontmatter with ---
Misusing curly braces syntax
5. How can you display a list of items defined in frontmatter inside the template in Astro?
Choose the correct code snippet to render a list of fruits stored in fruits array.