Remove extra carriage returns between fields after using $page->update()

After using the $page->update() function to change some fields via a frontend form, the format of the page.txt file changes from:

title: My blog post
color: green
text: Hello there, how are you?


title: My blog post


color: green


text: Hello there, how are you today?

Although the extra carriage returns between fields and separators don’t affect anything programming-wise, they make reading the page.txt file a bit less comfortable. They’re also inconsistent with the condensed format used throughout the Docs.

Is this something that could/should be changed either manually or via a tweak to /core/page.php?

As you wrote, the two formats are identical for Kirby as Kirby trims all whitespace at the start and end of the field when loading the content files.

If you don’t want to have the newlines, you would have to change this line in the Toolkit Data class. You don’t have to modify the Kirby code though. Instead you can put this into your config.php to override the built-in function:

data::$adapters['kd']['encode'] = function($data) {

  $result = array();
  foreach($data AS $key => $value) {
    $key = str::ucfirst(str::slug($key));

    if(empty($key) || is_null($value)) continue;

    // avoid problems with arrays
    if(is_array($value)) {
      $value = '';

    // escape accidental dividers within a field
    $value = preg_replace('!(\n|^)----(.*?\R*)!', "$1\\----$2", $value);

    // multi-line content
    if(preg_match('!\R!', $value, $matches)) {
      $result[$key] = $key . ": \n\n" . trim($value);
    // single-line content
    } else {
      $result[$key] = $key . ': ' . trim($value);        

  return implode("\n----\n", $result);


Great - adding that to config.php worked. Is there a reason this isn’t the default?

It’s just a matter of taste. As far as I know, the “official” format changed with Kirby 2 because it separates the fields more clearly, but if you like the condensed format better, go for it. We probably won’t change the default though.

1 Like

I guess it’s sort of like the spaces vs tabs debate. After seeing the condensed format used throughout the Docs I guess seeing the new official format just threw me off.

Thank you for your help.

Yes, I think it’s what you are used to. I used to write my text files in the condensed format as well, and found the carriage returns a bit annoying. But then I started to write Kirby documentation, and I quickly got used to the format with extra lines, and now I actually prefer having that extra bit of space.