1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Create pages from data in eleventy

    Khaled GarbayaKhaled Garbaya
    11ty11ty

    In this video, you will learn how to create pages from data in eleventy. a data source can be a JSON file or a js file.

    for more information check out the eleventy docs

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: 0:01 First, let's create a folder called _data. Inside of the data file create a Pokemons.JSON. In here we'll have an array of Pokémons. The first one will be Pikachu. The power will be electricity. The second one will be Tresar and the power will be fire.

    0:29 Let's save this. Now go to the root of the project and create a file. Call it Pokémonpage.mjk. Let's add some front metadata. First, we need to have pagination. Inside of that we need to give it the data source, in this case it will be Pokémons, the same as our JSON file name.

    0:55 The size, it will one. We'll be displaying only one per page. The alias here will be pokemon, so for a single Pokemon.

    1:07 Now we need to define the URL for every page. In here, we can type permalink, we want that in pokemons/. Then here we'll pick the Pokemon name. Then we'll pass it through the filter slug to make it lowercase. Then here, add / and then close the quote.

    1:32 For every Pokemon, you want to display the name and the power. Let's go here and define the p tag. Then here we'll have Name, pokemon.name and close our tag. In here, we will have Power.

    1:50 Now let's check our logs. You can see here, we already created some pages for our Pokemon. Let's go to /pokemons/charizard. You can see here, we have the Name and the Power. We can also pick Pikachu. You can see here, we're displaying the name from our JSON file.