⚠️ This lesson is retired and might contain outdated information.

Create pages from data in eleventy

Khaled Garbaya
InstructorKhaled Garbaya
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 2 years ago

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

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.

egghead
egghead
~ 29 seconds ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today