Field method trick - Use collections


When using the field method in Kirby, the field method can only take one field.

$page->my_field()->my_method( $my_args );

In my case I had a whole collection of fields that I wanted to use.

Solution - Use collections

The template

You are allowed to use a field that does not exist. That way you can kind of set up your own function like this:

echo $page->get()->collection( array( 'field_key1', 'field_key2' ) );

get() should be a field, but it can be empty or not set. In this case the field does not exist in the content, but the collection() method is still called. It takes two field keys to be used in the method.

The collection method

The collection method has two field keys in the $array attribute. In this example it just loops them into an $out string which is returned to the template.

field::$methods['collection'] = function( $field, $array ) {
	$out = '';
	foreach( $array as $item  ) {
		$out .= $field->page->$item() . ' ';
	$field->value = $out;
	return $field;

If you want to try it out, make sure you have the fields add field_key1 and field_key2 in your content.

Sidenote - Use whatever field key you want

The code below would also work because what_ever_i_want is kind of ignored, as long as you don’t have that field in your content.

echo $page->what_ever_i_want()->collection( array( 'field_key1', 'field_key2' ) );


  • It’s a Kirby way of manipulating multiple fields.
  • Because the field method always include the $page object, it does not need to be sent to the method. That is used to extract the two fields by their field keys.
  • get() is kind of ignored and you can use whatever you want except built in functions and field keys.