All fields in array?

Hi,
how can I output all fields of an page in an array?
I found pluck(). But this is not exactly what I want. I want all from one item and not one field from all items.

Kind

<?php
$c = $page->content();
foreach ($c as $key => $value) {
  if ($key == "fields") {
    foreach ($value as $field) {
      echo $field . ": " . $page->$field() . "<br/>";
    }
  }
}
?>

originally copied from this post http://getkirby.com/forum/general/20141007/how-to-loop-through-all-fields-on-a-page, not tested

3 Likes

Hey,
thanks for it!
As first I tested the output from $page->content(); but there I got the error β€žCatchable fatal error: Object of class Content could not be converted to string inβ€¦β€œ. Whats going wrong here?
Here is my code:

<?php foreach($page->children() as $section): ?>
<section class="text list">
	<?php foreach($section->children() as $book): ?>
	<article class="list-item">
	<div><?php echo $book->content(); ?></div>
		<?php if($book->hasImages()) : ?>
…

$book->content() returns an object, you cannot echo that …

1 Like

Hi,
your posted code works perfekt for me. Thanks!

You can get an array of the whole page array with

$page->content()->toArray()

or the whole page object (also including e.g. uid, slug, intendedTemplate) with

$page->toArray()
1 Like