Skip to content

Kirby 4.5.0

$blocks->when()

The when method only executes the given Closure when the first parameter is true. If the first parameter is false, the Closure will not be executed.

$blocks->when(mixed $condition, Closure $callback, ?Closure $fallback = null): mixed

Parameters

Name Type Default
$condition * mixed
$callback * Closure
$fallback Closure|null null

Return type

mixed

Parent class

Kirby\Cms\Blocks inherited from Kirby\Toolkit\Collection

Example

Filter by type if type is given:

$type  = get('type');

$blocks = $blocks
->when($type, fn ($type) => $this->filterBy('type', $type));