Retrieve Collections using the Content Rep API

Hi there,

I’m looking to use Kirby’s collection feature. I’m retrieving data from Kirby using Vue + the Content Rep API. I’m fine when fetching page-specific json using Axios but I’m unsure how to the same for collections, is this possible?

Below is what I have so far in kirby/site/collections/latest.php


return function ($site) {

    return $site->find('shows')->children();

I would assume that retrieving the $site variable would give me what I need. But I only get the Title, children ( pages ), and routes in the response.

Any help would be much appreciated!

I assume you mean content representations? If not, what exactly do you mean?

You would have to expose that collection via your content representation in this case.

I do mean Kirby Content Representations API yes.

Would I do that within the collections folder in the same way that I do with template files?

Once that’s done would I expect to the result within the $site object?

No, there are no content representations for collections. A Content Representation is a non-html representation of an HTML template, so xxx.json.php returns JSON etc. You will have to return the collection from the JSON template to be able to consume it from your Vue frontend.

Great! I think that answers my question.

Just wondering how the $site variables data is created? If it is not coming from a template? Is it done internally within Kirby’s core?

Some variables, like $kirby, $site, $page and $page are globally available in controllers and templates/snippets. In collections, you can pass the $site and $usersvariables as argument to have them available within the anonymous function, I think.

In plugins, hooks etc. you would have to use the helpers kirby(), pages(), page() etc. to access those objects.

1 Like