Get data from field (json)

Hello, I am very new to Kirby and I am a frontend dev, PHP is not my strong suit.

What am I doing wrong here? How do I get the input content for ‘carousel0’ field?

My template has this:
fields: carousel0: label: Carousel 1 title type: textarea width: 2/4

My homeapi.php:
$json = array( 'title' => (string)$page->title(), 'carousel0' => (string)$page->get('carousel0') );

It outputs:
{"title":"Kirby Starterkit","carousel0":null}

Should be:

$json = array(
'title' => (string)$page->title(),
'carousel0' => (string)$page->carousel0()

The $page->get() method does not exist.

Tried that before, but it returns:
{"title":"Kirby Starterkit","carousel0":""}

and I am sure the field has content:

Could you please post the content of the content/home/home.txt file?

Maybe your field is called differently, it should work if the field exists and if it has content.

I guess it’s getting there, this is what I have and I guess this is the worng part:

content/home/home.txt (is the one updated by the cms)
content/home/api/homeapi.txt (does not reflect the changes to home.txt)

Ok, so you are on the homepage and want to fetch the contents of the homeapi page?

$json = array(
'title' => (string)page('home/api')->title(),
'carousel0' => (string)page('home/api')->carousel0()



I think it’s the other way around, calling the /home/api URL (so the page data will be from content/home/api/homeapi.txt) and trying to show data from the home page (content/home/home.txt).

You could write a route config entry + function:

Or even a generic JSON route:

// Pattern: ([a-z0-9\/]).json
function($uri) {
  $page = page($uri);
  if (!$page) return false;
  // or make a Response object using fields from $page,
  // converted to JSON

@fvsch, yes, you are right, I completely overlooked that; it’s about the homeapi.php.

So in the homeapi.php you would then fetch the content from the home page like this:

$json = array(
'title' => (string)page('home')->title(),
'carousel0' => (string)page('home')->carousel0()

If you use a route as @fvsch suggested is more versatile and you don’t even need the api folder.

1 Like

For instance with this you can return the raw field data for any page in the site as a JSON response:

c::set('routes', [
   // Return a page's full content as JSON, for any page.
   // (Would need a bit more work for a multilingual site.)
      'pattern' => '([a-z0-9\\/]+).json',
      'action'  => function($uri) {
         $page = page($uri);
         if (!$page) {
            $content = '{}';
            if ($errorPage = page(c::get('error', 'error'))) {
               $content = ['title' => $errorPage->title()->value];
            return new Response($content, 'json', 404);
         return new Response($page->content()->toArray(), 'json', 200);

Calling /home.json, /some/other/page.json etc.

Of course you will probably want to be more specific. Eg. only returning a JSON response for some pages or only returning some of the fields (perhaps even with some transformations, like returning HTML content instead of Markdown/Kirbytext).

1 Like

this made me realise I don’t need: /content/home/api/…

I am only using kirby to export the json, all I needed was to write the json output on /templates/home.php, now everything works well.

Thanks a lot you all!