Undefined offset (YAML parsing error)

I’m creating a plugin that imports content into Kirby and I ran into an issue. At first, I thought that my importer isn’t working properly, but it’s actually Kirby’s YAML parser.

When I import content, some values may be empty. This would result in the creation of the following data inside the content folder:


  lcitembody: |
  lcitembody: |
  lcitembody: |

When I open a page that uses this data, I get:

Whoops\Exception\ErrorException thrown with message “Undefined offset: 6”

#19 Whoops\Exception\ErrorException in C:\xampp\htdocs\project\kirby\vendor\getkirby\toolkit\vendors\yaml\yaml.php:508
#18 Whoops\Run:handleError in C:\xampp\htdocs\project\kirby\vendor\getkirby\toolkit\vendors\yaml\yaml.php:508
#17 Spyc:loadWithSource in C:\xampp\htdocs\project\kirby\vendor\getkirby\toolkit\vendors\yaml\yaml.php:474
#16 Spyc:__load in C:\xampp\htdocs\project\kirby\vendor\getkirby\toolkit\vendors\yaml\yaml.php:152
#15 Spyc:YAMLLoad in C:\xampp\htdocs\project\kirby\vendor\getkirby\toolkit\lib\yaml.php:45
#14 Yaml:decode in C:\xampp\htdocs\project\kirby\vendor\getkirby\toolkit\helpers.php:292
#13 yaml in C:\xampp\htdocs\project\kirby\extensions\methods.php:154
#12 Kirby:{closure} in C:\xampp\htdocs\project\kirby\vendor\getkirby\toolkit\helpers.php:282
#11 call in C:\xampp\htdocs\project\kirby\core\field.php:110
#10 FieldAbstract:__call in C:\xampp\htdocs\project\kirby\extensions\methods.php:269
#9 Kirby:{closure} in C:\xampp\htdocs\project\kirby\vendor\getkirby\toolkit\helpers.php:282
#8 call in C:\xampp\htdocs\project\kirby\core\field.php:110
#7 FieldAbstract:__call in C:\xampp\htdocs\project\site\templates\instant-crypto-loans.php:187
#6 require in C:\xampp\htdocs\project\kirby\vendor\getkirby\toolkit\lib\tpl.php:22
#5 Tpl:load in C:\xampp\htdocs\project\kirby\kirby\component\template.php:103
#4 Kirby\Component\Template:render in C:\xampp\htdocs\project\kirby\kirby.php:681
#3 Kirby:template in C:\xampp\htdocs\project\kirby\kirby.php:669
#2 Kirby:render in C:\xampp\htdocs\project\kirby\kirby\component\response.php:29
#1 Kirby\Component\Response:make in C:\xampp\htdocs\project\kirby\kirby.php:751
#0 Kirby:launch in C:\xampp\htdocs\project\index.php:16

I import the content through a Kirby plugin, using the Kirby router and Kirby’s core functions for updating pages. I also use Kirby’s core YAML class for reading and writing YAML. The content above was generated with that class and saved in the page content with $page->update().

After further digging, I found an online YAML parser where pasting this:

  lcitembody: |
  lcitembody: |
  lcitembody: |

Works as expected and creates an array with three objects containing an empty lcitembody property. Same thing happened in this parser and this one.

So it appears that the problem is specific to Kirby.

Edit: Before entering Kirby, the lcitembody fields have values \n\n\n\n because reasons. That’s probably why they are saved with a pipe | instead of empty quotes "". That doesn’t change the fact that the parser fails to parse something that it should be able to.

Have you tried using quotes (e.g. "") for empty values?

Tell that to the Kirby YAML class since it generated that content. The reason for this problem is probably because my data has values like \n\n\n\n and the class thinks that’s multiline text when in reality, it’s an empty string. So instead of "", the parser puts | and later when that content is parsed, the class fails to parse it.

I don’t know if the class should detect that the string is empty and use "" instead of |, but it should definitely be able to parse an empty property using |, according to other parsers. That’s a fault in the class. That’s the point of my thread also.

You could correct that manually for now and create an issue on GitHub.

1 Like