Spad: Site to json plugin

plugin

#1

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:

http://yourkirbysite.com/spad

The returned json is pretty simple: recursively includes visible page data and files. More details on the repository: https://github.com/jongacnik/spad

:metal:


#2

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: https://github.com/jongacnik/spad


#3

This is great, thank you.

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

niceSize for instance?


#4

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) {
    unset($file['niceSize']);
  }
  return $page;
});

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


#5

Works perfectly. Thanks.


#6

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:


#7

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?


#8

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.


#9

just sent a PR to update kirby’s v3 methods — https://github.com/jongacnik/spad/pull/3


#10

one problem left is to add:

  • status
  • template

keys into the function that outputs the json. kirby seems to do it in here https://github.com/getkirby/kirby/blob/master/src/Api/Model.php

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?

@texnixe

:thinking:


#11

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


Kirby 3 as Headless CMS for Vue/Nuxt