Content from an API
Enrich your site with content fetched from any API
For this example, we use a freely accessible API, in this case the New York Times Movie Review API. To follow this example, create an account and an example app by following the instructions.
First, create a parent page, e.g. reviews
in the /content
folder and inside it, a reviews.txt
text file.
This page will serve as our model for child pages.
The page model
In the Reviews
page model, we redefine the children()
method to get the subpages from the API instead of from the file system:
Replace api-key
with the API key you got for your app.
Using the Remote::get()
method, you connect to the API and fetch the results. Within the foreach loop, you feed the results into the $pages
array and finally pass it all to the Pages::factory()
method.
The overview template
The pages are now accessible in the template and you can loop through them like through a normal set of Kirby pages, using the fields defined in the content
array:
The child page template
For the children themselves, you can create their own review.php
template to access more defails:
The result
The result could then look something like this: