Get page content with parsed YAML?

I can use $page->content()->toArray() to get the page’s fields in an array. However, structure fields are still in YAML format (strings). Is there a way to get them parsed with the Kirby API? And I don’t mean with yaml::decode because you can’t know which field is a structure and which is just a string. You’d have to dig in the blueprint to find out.

I’ve made this method:

public function parseContent ($content, $fieldBlueprints) {
  $data = [];

  foreach ($fieldBlueprints as $key => $blueprint) {
    $field = $content->$key();

    if ($blueprint['type'] === 'structure') {
      $data[$key] = $field->yaml();
    } else {
      $data[$key] = $field->value();
    }
  }

  return $data;
}

and then you call that with:

$this->parseContent($page->content(), $page->blueprint()->fields());

It works, but my question is - is there a way to do that with the Kirby API? I mean, at some point, Kirby has to parse that YAML when it renders the page, so that functionality must be somewhere there. If it’s not, then perhaps I should propose a $page->parsedContent() method.

Kirby renders the page according to what is in your template, so no, I don’t think there is a method that does that automatically. The yaml is parsed when you call toYaml() or toStructure() on a field.

The blueprints are only read to display the forms in the Panel.

Oh, oops, I entirely forgot how Kirby functioned for a second there. So what I’m doing is the correct way to do it. Do you think it’s worth opening an issue for that in the ideas repo? Does it have a chance of getting implemented?

You can always put it into the ideas repo. On the other hand, it is no problem to create a custom method if this is really needed. What is your use case?