Export content fields to CSV

I am upgrading an old site in a much leaner and meaner code base, and im trying to migrate the old content in to the new site. I am trying to save to CSV (automatic download for now but would rather save straight to disk). From there I am going to use the CSV handler plugin (Thanks @texnixe) to bring the content into the new site.

I based this on another forum post:

c::set('routes', [
  [
    'pattern' => 'api/csv',
    'action' => function() {
      header::download(['mime'=>'application/csv', 'name' => 'blogpages.csv']);
      $blogpages = site()->index()->find('blog')->children()->visible();

      $blogpagefields = array();

      foreach ($blogpages as $bpf) {

        $blogpagefields[] = array(
          'Title' => $bpf->title(),
          'Date' => $bpf->date(),
          'Author' => $bpf->author(),
          'Featurehome' => $bpf->featurehome(),
          'Featured' => $bpf->featured(),
          'Text' => $bpf->text(),
          'Categories' => $bpf->categories(),
          'Tags' => $bpf->tags(),
          'TitleTag' => $bpf->title-tag(),
          'Description' => $bpf->description(),
          'Keywords' => $bpf->keywords(),
          'Modified' => $bpf->modified(),
          'Schedule' => $bpf->schedule(),
        );

      }

      $f = fopen('php://output', 'w');

      foreach($blogpagefields as $blogpagelist) {
          fputcsv($f, $blogpagelist, ';');
      }

      fpassthru($f);
      return false;
    }
  ]
]);

However, when I go to domain.test/api/csv I get an error saying the page cannot be found (server page, not a woops or some other Kirby error - the server itself is saying it cant be reached). The rest of the site itself works. What have done wrong?

There’s an error in this line above; you probably mean titletag() or title_tag(). In any case, you can’t use a dash in a method call.

Interesting. It’s a new one on me. I just wrote it as it was declared in the content file. I did not build site in the first place, I keep things simple when I name them. Every day is a school day :slight_smile:

Good spot, thanks.

The dash is used in the content file, but you nevertheless have to use an underscore to fetch it’s content in your templates.