https://github.com/jongacnik/kirby2-spad
📄 Kirby single page app json
https://github.com/jongacnik/kirby2-spad
api json kirby single-page-app spa
Last synced: about 1 month ago
JSON representation
📄 Kirby single page app json
- Host: GitHub
- URL: https://github.com/jongacnik/kirby2-spad
- Owner: jongacnik
- License: mit
- Created: 2016-09-26T00:10:26.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-02-19T14:08:16.000Z (about 6 years ago)
- Last Synced: 2025-02-28T09:21:02.816Z (about 2 months ago)
- Topics: api, json, kirby, single-page-app, spa
- Language: PHP
- Homepage:
- Size: 6.84 KB
- Stars: 47
- Watchers: 7
- Forks: 3
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# Spad
Spad is a [Kirby](http://getkirby.com) plugin to expose your site data as json for use in a single page app.
## Usage
Echos site data as json:
```php
= $site->spad() ?>
```Typically you'd use this to inline into a variable for use in your app:
```php
var data = <?= $site->spad() ?>
```## Response
Spad recursively includes visible page data and files. [**Click here**](#starterkit-response) to view the default response from the Kirby starterkit.
## Filters
Two filters are provided to filter the json response:
```php
c::set('spad.filterAll', function ($data) {
return $data;
});c::set('spad.filterEach', function ($page) {
return $page;
});
```These are useful if you need to perform some transformations on the data or to remove unecessary fields. `filterAll` is run at the very end for filtering the entire tree, whereas `filterEach` is run on each page. Here is an example that removes some of the fields from each page:
```php
c::set('spad.filterEach', function ($page) {
$dontNeed = [
'parent',
'dirname',
'diruri',
'contentUrl',
'tinyUrl',
'depth',
'root',
'uid',
'num',
'hash',
'modified'
];foreach ($dontNeed as $field) {
unset($page[$field]);
}return $page;
});
```You can use page templates to *conditionally* filter pages:
```php
c::set('spad.filterEach', function ($page) {
if ($page['intendedTemplate'] == 'project') {
// only do a transformation on pages with `project` template
}
return $page;
});
```**Note:** Careful not remove the `child` field when using `filterEach`, otherwise you will break recursion!
## Route
For handiness, Spad also adds a route which returns the site as json:
```bash
http://yourkirbysite.com/spad
```## Options
Options are provided to customize the method and route names, and to filter the data:
```php
\r\n\r\n## Follow us on Twitter\r\n\r\n- Follow Kirby on Twitter (twitter: @getkirby)\r\n- Follow Bastian on Twitter (twitter: @bastianallgeier)\r\n- Follow Nico on Twitter (twitter: @distantnative)\r\n- Follow Sascha on Twitter (twitter: @sashtown)"
},
"files":[],
"children":[]
}
]
}
```