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!

5 Likes

Hello again, Kirby community! :wave:

I’ve just released version 1.4.0 of Field Composer, adding two helpful debugging methods to the plugin, $field->dump() and $field->log().

New Features

  • field->dump(): Allows you to use Kirby’s dump() method within a method chain and can help you debug complex chains by outputting values at any point in the chain. The dump message can be customized with several parameters.
// dumps the result of merged artist and title to the page and then
// continues with the method chain
$page->artist()->merge($page->title())->dump()->upper();
  • field->log(): The new log() method creates timestamped logs of field values:
// Logs to site/logs/field_composer.log
$page->price()->log('Price: {{ val }} $')->upper();
// => adds a log like `[2024-11-10 15:23:19] Price: 23.49 $` to the log file

The log method is particularly useful when debugging field operations in contexts where output cannot be displayed, such as in panel query strings or on production servers.

Both methods work seamlessly with the plugin’s existing field composition features and maintain chainability:

$page->title()
  ->merge($page->subtitle())
  ->dump('Combined title: ')
  ->wrap('«', '»');

Hope the plugin will be useful for you!