Add a headless CMS to Ember in 5 minutes
Storyblok is the first headless CMS that works for developers & marketers alike.
In this short tutorial we show you how to integrate the Storyblok API into an Ember App. We build the components step by step and develop the integration using Storyblok API and Storyblok SDK. This is the final result:
You can clone this tutorial at https://github.com/storyblok/storyblok-ember-boilerplate
Environment Setup
Requirements
- Understanding of Ember
- Node, yarn (or npm) and npx installed
- An account on Storyblok to manage content
- A space already configured in Storyblok
Setup the project
We use ember-cli to generate the initial setup for our project and to generate the routes, components and other things. To generate the initial project, execute the new
command:
This command will config the Ember application in storyblok-test
folder and install ember-fecth package.
The next step is to load all of the data from Storyblok API into your Ember application. For this, it is necessary to create a index route:
After this, edit the app/router.js
file to map the index route as a dynamic route with path param like this:
The index route will receive a path as a parameter. This is useful when we get data from the Storyblok API
Creating a service to connect the Storyblok API
Start by creating a service to put in a Storyblok API connection logic. Execute the generate service command:
This command creates a storyblok.js
file into the app/services
folder. Then edit it:
With the service created, edit the app/routes/index.js
file to get this path parameter and use it to load data from Storyblok API:
The model function is executed before the route loads and puts the content in the route template. In that case, it will load a specific story from Storyblok API and put it in the template.
Checking what we've done so far
To know if the data is loading correctly, execute the Ember serve command on the terminal using npm or yarn:
Open your browser in http://localhost:4200/home and you should see the Ember welcome page. Modify this to show the page name.
Edit the app/templates/application.hbs
by removing all content less than {{outlet}}
expression. After that, edit the index.hbs
file to show the page name in the h1 tag.
Open your browser again in http://localhost:4200/home. You should see the 'Home' name like this:
Setup the Storyblok components
Now create some components to render the home page correctly.
Teaser component
First, create the component files by using the Ember generate command:
Then, edit the teaser.hbs
file to this:
Grid component
Repeat what was done with the teaser component: execute the generate command and edit the grid.hbs
file:
Edit the grid component using the component helper to render the correct component that came from the Storyblok API
Feature component
The last component (for now) is the feature component. Execute in your terminal:
Edit the feature.hbs
file:
Editing the index.hbs route
Finally, edit the index.hbs
file to iterate over our components and render them correctly:
Adding some styles
Before seeing what it looks like, try adding some styles. Add this CSS link in the app/index.html
file and edit the app/styles/app.css
to it:
Open your browser again in http://localhost:4200/home:
About page
Create a new page called 'About' in our Storyblok space. Then add two components on schema: a Teaser
component and a Markdown
component. The last one will be a Richtext component with a simple Lorem Ipsum text.
Creating a Navbar component
Before setting up the About page in this project, create a Navbar
component to switch between pages. Execute this command in your terminal:
Edit the navbar.hbs
file like this:
Add this component in the index.hbs
file:
And lastly, add some styles in app.css replacing the content with the following:
Now open your browser and check if the Home page looks like this:
Go to the About page by clicking the About link in navbar... What do you see? Hopefully you see the Navbar links and an About title. But where is the Loren Ipsum text from Markdown? If you open the console's browser there will be an error message saying that the 'markdown' component name does not exist. Let's fix this!
Set up the Richtext component
You can find more information about the Rich Text component and how to you use it in our documentation. For now, use our Javascript SDK with the resolver to transform the complex JSON structure from Storyblok into HTML that we will use for our page.
Execute the generate component command in our terminal:
Install the Storyblok Javascript SDK:
Edit the markdown.js file creating an Ember file that includes the transformations that we need.
Finally, edit the markdown.hbs
file to show that the HTML has been transformed.
You see that we use the Storyblok service that we created before. Now, replace the storyblok.js file with the following content:
Open your browser in http://localhost:4200/about and see the correct content:
Conclusion
In this tutorial you learned how to integrate Storyblok API in an Ember framework. You created some components and used the Ember Service to integrate the Storyblok data into an Index route. You can clone this tutorial at https://github.com/storyblok/storyblok-ember-boilerplate too.