Spad: Site to json plugin

Whipped up a little plugin called Spad (single page app data) which adds a method and route to return all your site data as json. Might be handy for inlining data into a variable for use in front end apps:

<script>var data = <?= $site->spad() ?></script>

The route to return site as json response is at:

The returned json is pretty simple: recursively includes visible page data and files. More details on the repository:


1 Like

Updated the plugin with filtering methods to tailor the json response:

c::set('spad.filterAll', function ($data) {
  return $data;

c::set('spad.filterEach', function ($page) {
  return $page;

Also improved readme contents with an example json response from the Kirby starterkit:

This is great, thank you.

One question about filtering. Is it possible to remove fields in children/files?

niceSize for instance?

You should be able to. You could use spad.filterEach to filter each item, and do something like (untested):

c::set('spad.filterEach', function ($page) {
  foreach ($page['files'] as &$file) {
  return $page;

In the example above I’m passing the array by reference to directly modify the items.

Works perfectly. Thanks.

for the people who used it: any example on the frontend js tool you used this plugin with?

getting confused on how to pass the json object to tools like choo.js, and yes im just starting to play with node.js — so that makes it double confusing :smiley:

looks like that using this plugin with kirby3 throws an error on L29:

$kirby->set('site::method', kirby()->option('spad.method', 'spad'), function () use ($spad) {
  return json_encode($spad());

has the set method being deprecated?

Depending on your needs, you may not need a plugin to serve your page data as JSON - have a look at this Cookbook recipe. Kirby has the capacity of serving content data in multiple formats, built-in.

just sent a PR to update kirby’s v3 methods —

one problem left is to add:

  • status
  • template

keys into the function that outputs the json. kirby seems to do it in here

not sure if this is due to the fact that the spad plugin does not authenticate, and maybe these two extra keys are kept behind login?



Hey @afincato thanks for taking interest in the plugin. It is quite old and as @luxlogica pointed out, there are more native ways of doing this type of thing now. All the plugin really does is generate an object by recursively looping through pages.

As for the missing keys, it is possible the $page->toArray() method no longer includes them. You’d have to merge them in manually if that is the case (using page methods). In general, might be best to open issues like this on github itself.

Cheers, J

1 Like