Get rid of loops because of powerful Kirby methods

For me personally, if I don’t need a loop, I don’t want to have loop.

The Kirby methods are so powerful nowdays, that we don’t always need loops for everything. I’ll give you an example of a successful test I did.

Case

Pages with time in a field

I wanted to get the item 2000-01-11, 01:01:01 from this collection:

2016-09-19, 06:55:00
2000-01-11, 01:01:01
2016-09-19, 06:55:00

2016-09-19, 06:50:00

To do that I needed to:

  • Get rid of empty values
  • Sort by number
  • Sort ascending
  • Only get a single item

Full code

  • No custom filter collection needed
  • No loops needed

In my own code I have this code in only 3 lines but this forum don’t break the row, so here it’s 4 rows.

$excluded =  $companies->children()->filterBy('status_modified', '==', '');
$companies = $companies->children()->sortBy('status_modified', 'asc', SORT_NUMERIC );
$company = $companies->not($excluded )->first();
echo $company->status_modified();

No loops! Amazing! :slight_smile:

I really like the feature Pass collection as argument in the not method.

Should be possible with a one liner:

$company = $companies->children()->filterBy('status_modified', '!=', '')->sortBy('status_modified', 'asc', SORT_NUMERIC )->first();
```
2 Likes