# Pre-format information from page fields for a clean template

**URL:** <https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854>\
**Category:** Questions\
**Tags:** v2\
**Created:** [December 19, 2018, 8:55pm UTC](https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854 "2018-12-19T20:55:56Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![bruno](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bruno/32/6422_2.png) [@bruno](https://forum.getkirby.com/u/bruno)\
**Post date:** [April 24, 2019, 9:19pm UTC](https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854/21 "2019-04-24T21:19:07Z")

</div>

Oh yeah – that’s true! It’s still using the old home.php controller, I had commented out the part that was throwing the error. Sent you the updated file!

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 24, 2019, 9:56pm UTC](https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854/22 "2019-04-24T21:56:04Z")

</div>

Hm, yes, ok, that doesn’t look good. I don’t know what’s happening there.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [April 25, 2019, 6:36pm UTC](https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854/23 "2019-04-25T18:36:33Z")

</div>

@bruno It just doesn’t work with `groupBy()`, no idea why, but does using `group()`:

```auto
<?php

date_default_timezone_set('Europe/Vienna');

return function($site, $pages, $page) {

    // $groups = page('exhibitions')->children()->visible()->sortBy('startDate', 'desc')->group(function($exhibition) {
    // $now = new DateTime('now');
    // $start = new DateTime($exhibition->startDate());
    // $end = new DateTime($exhibition->endDate());
    //
    // if ($start > $now) return 'Upcoming';
    // if ($start < $now && $end > $now) return 'Current';
    // return 'Past';
    //
    // });

  $groups = page('exhibitions')
    ->children()
    ->visible()
    ->sortBy('startDate', 'desc')
    ->map(function($exhibition) {

      $now = new DateTime('now');
  $start = new DateTime($exhibition->startDate());
  $end = new DateTime($exhibition->endDate());

  if ($start > $now) {
    $exhibition->category = 'Upcoming';
    $exhibition->sortOrder = 0;
  }
  if ($start < $now && $end > $now) {
     $exhibition->category = 'Current';
      $exhibition->sortOrder = 1;
  } else {
    $exhibition->category = 'Past';
     $exhibition->sortOrder = 2;
  }
  return $exhibition;
  })->sortBy('sortOrder', 'asc')->group(function($item) {
    return $item->category();
  });

  return [
    'groups' => $groups,

  ];

};

```

Note that there’s an undefined variable in your home template (`$time`)

Also, I removed the additional mapping stuff at the end because I don’t know what it was there for.

---

<div class="post-metadata">

**Author:** ![bruno](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bruno/32/6422_2.png) [@bruno](https://forum.getkirby.com/u/bruno)\
**Post date:** [May 1, 2019, 9:12pm UTC](https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854/24 "2019-05-01T21:12:20Z")

</div>

Thank you! Hmm, the code is not “broken” anymore but it is putting all exhibitions into the “Past”-category and sorting them alphabetically by title… did you change anything in any other files as well?  
(`$time = $description` is defined on line 14 – the mapping stuff was just some commented out trials from before.)

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 2, 2019, 6:04am UTC](https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854/25 "2019-05-02T06:04:47Z")

</div>

Ah, I missed the logic error when I pasted here:

```auto
  if ($start > $now) {
    echo $exhibition->title();
    $exhibition->category = 'Upcoming';
    $exhibition->sortOrder = 0;
  } elseif ($start < $now && $end > $now) { # this must be else if
     $exhibition->category = 'Current';
      $exhibition->sortOrder = 1;
  } else {
    $exhibition->category = 'Past';
     $exhibition->sortOrder = 2;
  }
  return $exhibition;
  })->group(function($item) {
    return $item->category();
  })->sortBy('sortOrder', 'asc');

```

I think you also had some manual `sortorder` fields in your content files that might mess things up.

---

<div class="post-metadata">

**Author:** ![bruno](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bruno/32/6422_2.png) [@bruno](https://forum.getkirby.com/u/bruno)\
**Post date:** [May 2, 2019, 6:10am UTC](https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854/26 "2019-05-02T06:10:00Z")

</div>

Oh of course! The grouping works now – but the exhibitions are still being sorted alphabetically instead of chronologically.  
I added the `sortorder` fields because all content needed to have the field defined in order for the code to work. They all have a value of 1

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [May 2, 2019, 6:18am UTC](https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854/27 "2019-05-02T06:18:13Z")

</div>

I made a change above, the `sortBy('sortOrder')` bit should be at the end, so that it applies to the groups, not the items. The items themselves are already sorted at the top before the mapping stuff.

---

<div class="post-metadata">

**Author:** ![bruno](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bruno/32/6422_2.png) [@bruno](https://forum.getkirby.com/u/bruno)\
**Post date:** [May 2, 2019, 6:38am UTC](https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854/28 "2019-05-02T06:38:12Z")

</div>

👍 Thank you sooo much for helping me with this!

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:22pm UTC](https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854/29 "2025-02-06T17:22:46Z")

</div>



[Previous page](https://forum.getkirby.com/t/pre-format-information-from-page-fields-for-a-clean-template/11854.md?page=1)
