Iterating over external data

I’ve seen a few mentions of how to read external data on this forum, mostly in the form of JSON, XML, or similar streams not native to Grav at the moment. Setting it up in Javascript is quite easy, but resource-wise this is heavier and it does not allow for server-caching.

I am experimenting with importing images from Flickr (preferred over Instagram, who’s API is getting very restrictive) and their API is very easy to utilize. The problem I am having is doing this in Twig, in as much as that is possible, and of course ideally caching these images or the stream as a whole.

Has anyone done this on their Grav site? That is, accessed an external data source, iterated over this data, and stored it locally? What are preferred formats, and how do you treat it in Twig?

You really are best off interacting with the data via the API in a plugin, then setting the resulting array as a Twig variable. Then it’s very easy. Most of the plugins in the GPM/Downloads follow this mechanism to get data, set it on Twig, then use a Twig template to iterate over the data and output it. The GitHub plugin comes to mind as it’s doing exactly this, even down to using an API and caching the results so you don’t burn up your requests.

I’ll have a closer look at the GitHub plugin and see if I can twist it into either a Flickr-plugin or an API-reader implementation.