Features of Astro
First of all, I assume that you have already completed the necessary setup steps (such as installing the Astro extension) to get to this point. If you’ve done that, you’re now ready to start developing Astro files. Let’s get started without further ado.
Creating the Basic Template
First, create a working folder. Then, while inside the folder, right-click and open the terminal, or open the terminal of your editor if it has one (I’m using VS Code as an example), and enter the following commands in the terminal:
It will ask you where you want to create your new project. You can type “dot” (.) here and press Enter to select the folder where the terminal is currently running. If you type something like ”./FolderName,” it will create a folder with the name you specified inside the current working folder. I chose to use a dot:
Next, it will ask you, “How would you like to start your new project?” and provide three options:
Here, we choose the first option. After pressing Enter, you will need to wait for the files to be copied. The speed of this process depends on your computer and internet speed.
After this step, it will ask you, “Do you want to install the dependencies?”
We choose “Yes” here. (Note: You can choose “No” if you want to install the dependencies later.) Press Enter and wait for the dependencies to be downloaded. If there is an error during the download, it’s okay; you can download the missing files later (I’ll explain this in the continuation of the article).
If you’ve completed this step successfully, it will ask you whether you want to use TypeScript. For now, we choose “No.” (Note: You can enable TypeScript later if you wish.)
In the next step, it will ask if you want to create a GitHub repo. If you want to create a GitHub repo, you can choose “Yes.” I chose “No.”
If you encountered an error while installing dependencies, you can run the following command in the terminal:
Now that we’ve completed all the steps, we can start our project. You can start the project by typing the following code:
Once you’ve started the project, you will see the following lines on the screen:
Congratulations! You have successfully created your first Astro project. You can now access your project at http://localhost:3000/.
Note: If another application is running on port 3000 or if port 3001 is in use, it will run on port 3002. Be sure to check the terminal after running the program!
When you access this address, you will likely see a page similar to the one below:
Slots
In Astro, when you create a project, open the src/pages/index.astro file. This file is your main page. When you open this file, you will see the following code:
Now, let’s examine this code. Astro files have a structure, and this file gives you an idea of how Astro files are structured. The code between the three dashes --- is evaluated on the server-side. In this file, imports like Layout and Card are brought in between these dashes. What’s the purpose of this? It’s to avoid repeating code and save time by reusing it.
You might wonder why we write HTML code within the <Layout>
tags in this file. Let me explain: In the src/layouts/Layout.astro file, which is located in the Layout folder, you will see a <slot />
tag. The purpose of this tag is to place the code from src/pages/index.astro inside the <slot />
tag of src/layouts/Layout.astro. This way, the code in src/layouts/Layout.astro combines with the code in src/pages/index.astro to create a single page. As a result, we separate the code in src/layouts/Layout.astro from the code in src/pages/index.astro, creating a more organized code structure.
Modular File Structure
With a modular file structure, you can write your code more neatly and readably. It also allows you to reuse your code. For example, the code in src/components/Card.astro can be used in src/pages/index.astro, eliminating the need to rewrite the code. This saves you time.
For example:
This code in index.astro eliminates the need to repeat code.
Styles
Now, let’s look at how CSS is used in Astro files.
In Astro files, you can define CSS in two different ways: globally and scoped. Global CSS can be used by other Astro files, while scoped CSS is only used within the current Astro file.
Here’s an example that demonstrates both global and scoped styles:
Constants
Now, let’s look at how constants are used in Astro files.
In Astro, you define constants between the three dashes --- at the top of Astro files. Here’s an example:
In this code block, we defined a constant title and assigned it a string value. Then, we used <title>{title}</title>
within the <title>
tag to dynamically set the title of the page. If your project is running now and you’ve organized your code like this, you can see that the browser tab title changes accordingly.
However, don’t think that only string values can be defined as constants. You can define numeric values or arrays as well:
Map Loop
Now, let’s take a look at how the map loop works in Astro.
Let’s start by defining two arrays between the three dashes ---
:
Then, in the content of our page, we can use these arrays to render their items:
Notice that I used the map function within curly braces {}
. In Astro files, you can write certain JavaScript functions within curly braces.
Props
In AstroJS, you can use props to pass data from one Astro file to another.
For example, let’s create a component in the src/components/Baslik.astro file and pass data from it to the src/pages/index.astro file.
Here’s the content of the Baslik.astro file:
Now, let’s look at the content of the index.astro file:
In this code, we first import the Baslik component from the src/components/Baslik.astro file. Then, we use it as <Baslik title="Homepage"/>
within the body of the index.astro file. You can pass different objects, such as text, numbers, or arrays, by modifying these codes.
You can also pass objects to other files.
Conditional Rendering (if-else)
In this section, I’ll show you how to use conditional expressions in Astro files.
First, open your index.astro file and remove all the content. Then, add the following code:
In this code, we defined const {number=123} = Astro.props;
. This code sets the default value of number to 123. Since we provided a value for number, the if block will execute and display 123. However, if we hadn’t provided a value for number, the else block would have executed and displayed “No number.”
Map Loop
Now, let’s take a look at how the map loop is used:
Add the following code to your index.astro file:
In this code, we created an array numbers that contains sub-arrays. We then used a map loop to iterate through the sub-arrays and display them on the page.