Generating json

Hello,

https://getkirby.com/docs/cookbook/templating/generating-json
I try to recreate the cookbook example.

if I call https://website.com/programm.json can I then see the json? or do I have fetch it for example: with javascript ?
because I only see the error page

structur

  • home
  • about
  • programm -> events.txt
    • event1 -> event.txt
    • event2 -> event.txt

template

<?php

$data = $pages->find('programm')->children()->published()->flip();
$json = [];

foreach($data as $article) {

  $json[] = [
    'url'   => (string)$article->url(),
    'title' => (string)$article->title(),
    'text'  => (string)$article->text(),
    'date'  => (string)$article->date()
  ];

}

echo json_encode($json);

Just guessing; but shouldn’t you been using ->listed() instead of ->published()?

the template file of your is site/templates/events.json.php?
does a site/templates/events.php template exist?
does a blueprint site/blueprints/pages/events.yml exist and is valid (page editable in panel)?

@bnomei 3*yes

maybe there is an error in the file names

the name of the dir is “programm”
the textfiles “events” and “event”

the normal template-name is events.php
and for the json output events.json.php

what is the right url to see the json output
https://website.com/programm.json
or
https://website.com/events.json
?

should be this
https://website.com/programm.json

but make sure the regular template is working before setting up content representation…
https://website.com/programm

yes is working.

<main class="width-100 flex justify-content-center align-item-center">

    <?php foreach($page->children()->published() as $article): ?>

    <div><?php echo $article->title() ?></div>

    <?php endforeach ?>

    </main>

try removing the logic in the content rep. maybe there is an issue there?

<?php echo json_encode(['test']);

no still the error page.
my site is multi-language, can associate with it ?

Are you using the / root for the default language? Then => bug

1 Like

oh yes!

<?php

return [
  'code' => 'de',
  'default' => true,
  'direction' => 'ltr',
  'locale' => 'de_DE',
  'name' => 'Deutsch',
  'url' => '/'
];

now it works, thank you :smiley:
https://site.com/de/programm.json

<?php

    return [
      'code' => 'de',
      'default' => true,
      'direction' => 'ltr',
      'locale' => 'de_DE',
      'name' => 'Deutsch',
      'url' => '/de'
    ];