【问题标题】:Yaml frontmatter not parsing as documentedYaml frontmatter 未按文档解析
【发布时间】:2023-03-16 01:00:02
【问题描述】:

我在一个 php/Laravel 项目中安装了 yaml frontmatter,但我无法让它作为 documentend 工作。

我做了一个testfile test.html:

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

然后我尝试用yaml解析:

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

dd($document->title);

结果:

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

虽然问题应该是:

['title' => 'Example']

我做错了什么?

亲切的问候

休伯特

【问题讨论】:

  • Frontmatter 通常用在 Markdown 文件中,你试过用 test.md 代替 test.html 吗?
  • 没什么区别。
  • 无法复制。 dd($document->title); 应该输出only Example,你确定这是你正在运行的正确文件吗? dd($document) 会输出你的“结果”
  • 你是对的。实际上代码是 dd($document)

标签: php yaml


【解决方案1】:

首先将您的 test.html 文件移动到 resources 文件夹,然后按照此代码进行操作

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

dd($document->title);

我希望你这个解决方案是可行的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-25
    • 2015-09-25
    • 2014-11-05
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多