Yaml frontmatter not parsing as documented

Viewed 194

I have yaml frontmatter installed in a php/Laravel project, but I cannot get it to work as documentend.

I made a testfile test.html:

--- 
title: Example 
--- 
Lorem ipsum.

Then I try to parse with yaml:

$document = YamlFrontMatter::parse(file_get_contents('test.html'));

dd($document->title);

The result:

Spatie\YamlFrontMatter\Document {#280 ▼
  #matter: []
  #body: "--- title: Example --- Lorem ipsum."
}

While matter should be:

['title' => 'Example']

What am I doing wrong?

Kind regards

Hubert

2 Answers

First your test.html file move to resources folder now follow this code

$document = YamlFrontMatter::parseFile(resource_path('test.html'));

dd($document->title);

I hope you this solution is work.

In my case the 3 dashes had one space in front --- and that made the parser to no recognize the properties, when the space was removed all worked back again.

Related