Skip to content

Instantly share code, notes, and snippets.

@scottboms
Last active December 11, 2020 01:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottboms/06f4abee1bb0567501e6e1987e2b6908 to your computer and use it in GitHub Desktop.
Save scottboms/06f4abee1bb0567501e6e1987e2b6908 to your computer and use it in GitHub Desktop.
Example Kirby 3 recipe for creating a JSON Feed representation of a page with custom route
<?php
// Kirby site config file
// site/config/config.php
return [
'routes' => [
[
// This sets up two different URLs as an array that will
// return the JSON Feed representation of a page in Kirby
'pattern' => ['feed', 'some-other-path/feed'],
'action' => function() {
return page('some-page-name')->render([],'json');
},
'method' => 'GET'
],
],
]
<?php
// JSON representation template for a page
// called "some_page_name
// in site/templates/some_page_name.json.php
// I found this line was necessary despite the route
// having a json response type defined - YMMV
$kirby->response()->json();
// Set up some variables to handle the boilerplate
// info for the feed
$feed_title = "Title of the page, potentially pull from $page object";
$feed_description = "Description of the page content";
$feed_icon = "URL to an image";
$feed_favicon = "URL to a favicon image, ideally transparent PNG 64x64 minimum";
$feed_author = "Author full name";
$feed_author_image = "URL to an image, could be the same as the $feed_icon variable above";
// I use a controller to do the initial legwork of collecting an array
// of children from the page, returned here as $some_page_children with a limit applied
$feed = $some_page_children->limit(10);
// Loop through each of the items in the array returned
// and create the set of feed items
// see also: https://jsonfeed.org/version/1.1 for details
// but these are the basic required items + the optional date item
foreach($feed as $item) {
$entries[] = [
'id' => (string)$item->indexOf(),
'url' => (string)$item->url(),
'title' => (string)$item->title(),
'content_html' => (string)$item->text()->kirbytext(),
'date_published' => (string)$item->date()
];
}
$feed_json = [
'version' => 'https://jsonfeed.org/version/1',
'title' => (string)$feed_title,
'description' => (string)$feed_description,
'home_page_url' => (string)$site->url(),
'feed_url' => (string)$page->url() . '.json',
'icon' => (string)$feed_icon,
'favicon' => (string)$feed_favicon,
'authors' => [
'name' => (string)$feed_author,
'url' => (string)$site->url(),
'avatar' => (string)$feed_author_image
],
'language' => 'en-us',
'items' => $entries
];
echo json_encode($feed_json);
<?php
// Simple controller for the page
// in site/controllers/some_page_name.php
return function ($page) {
// Get all articles
$some_page_children = $page->children()->listed()->sortBy('date','desc');
return [
'some_page_children' => $some_page_children
];
};
<?php
// JSON representation template for a page
// called "some_page_name
// in site/templates/some_page_name.json.php
// Alternate version from above
// I found this line was necessary despite the route
// having a json response type defined - YMMV
$kirby->response()->json();
// Set up some variables to handle the boilerplate
// info for the feed
$feed_title = "Title of the page, potentially pull from $page object";
$feed_description = "Description of the page content";
$feed_icon = "URL to an image";
$feed_favicon = "URL to a favicon image, ideally transparent PNG 64x64 minimum";
$feed_author = "Author full name";
$feed_author_image = "URL to an image, could be the same as the $feed_icon variable above";
$feed_reply_link_open = "<hr><p><a href='mailto:feedback@domain.com?subject=Response to ";
$feed_reply_link_close = "'>Reply via e-mail</a></p>";
// I use a controller to do the initial legwork of collecting an array
// of children from the page, returned here as $some_page_children with a limit applied
$feed = $some_page_children->limit(10);
// Loop through each of the items in the array returned
// and create the set of feed items
// see also: https://jsonfeed.org/version/1.1 for details
// but these are the basic required items + the optional date item
foreach($feed as $item) {
$entries[] = [
'id' => (string)$item->indexOf(),
'url' => (string)$item->url(),
'title' => (string)$item->title(),
'content_html' => (string)$article->text()->kirbytext() . $feed_reply_link_open . $article->title()->html() . $feed_reply_link_close,
'date_published' => (string)$item->date()
];
}
$feed_json = [
'version' => 'https://jsonfeed.org/version/1',
'title' => (string)$feed_title,
'description' => (string)$feed_description,
'home_page_url' => (string)$site->url(),
'feed_url' => (string)$page->url() . '.json',
'icon' => (string)$feed_icon,
'favicon' => (string)$feed_favicon,
'authors' => [
'name' => (string)$feed_author,
'url' => (string)$site->url(),
'avatar' => (string)$feed_author_image
],
'language' => 'en-us',
'items' => $entries
];
echo json_encode($feed_json);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment