Kirby Field Composer - Easily compose complex strings from field values

Hi Kirby community! :wave:

I have finally published my first plugin, Field Composer.

Field Composer simplifies complex field operations in Kirby. It provides methods for merging fields, applying conditional logic, and manipulating strings all while handling field values intelligently to avoid unwanted formatting issues. This makes it easier to work with both simple and complex content structures.


Key Features

:test_tube:  Field Methods: A collection of methods to manipulate and compose field values.
:globe_with_meridians:  Global Helper Functions: field() and f() for easy field composition.
:dna:  Flexible Merging: Combining multiple fields with custom separators and positioning.
:label:  Smart Handling of Empty Fields: No separators get inserted when fields are empty.
:vertical_traffic_light:  Conditional Field Handling: Apply conditions to field rendering.
:abcd:  String Manipulation: Apply Kirby’s Str class methods directly to fields.


Usage

You can use it for really simple operations like merging field values …

$page->title()->merge($page->author(), $page->year());

… adding prefixes or suffixes, wrapping fields in strings or tags …

$page->title()->prefix('Coming soon: ', when: $page->release()->toDate() > time());

… rendering field values conditionally …

$page->price()->when($page->stock()->toInt() > 0)->or('Sold Out');

… or you can put it all together to compose some complex strings that would otherwise require lots of fiddling with conditional statements and implode statements.

field(
  [
    $artist->name()->or('Unknown'),
    field($artist->born(), $artist->died(), '-')
      ->prefix('*', when: $artist->died()->isEmpty()),
    $artist->birthplace()
  ],
  [
    $artwork->title()->or('Untitled'),
    $artwork->year()
  ],
  [
    $artwork->material(),
    $artwork->width()->merge($artwork->height(), ' × ')
      ->when($artwork->width(), $artwork->height())
      ->suffix(' cm')
      ->wrap('(', ')'),
    ''
  ],
  $artwork->collection(),
  $artwork->description(),
  '; '
);

Hope you like it and it helps you creating awesome websites!

4 Likes