modularr/yaml-front-matter

YAML-FrontMatter 是一个简单的 PHP-only 前置元数据解析库。

1.0.0 2018-04-05 21:41 UTC

This package is auto-updated.

Last update: 2024-09-10 07:55:53 UTC


README

Latest Version Software License Build Status Total Downloads

一个易于使用的类,用于处理 PHP 中的 YAML 前置元数据。

这个类做了什么?

这个类是 Jekyll 前置元数据 的 PHP 实现。

任何包含 YAML 前置元数据块 的文件都将被处理为特殊文件。前置元数据必须是文件中的第一部分,并且必须以 有效的 YAML 集合 位于三个短横线之间 的形式出现。以下是一个基本示例

---
layout: post
title: Blogging Like a Hacker
---

在这些三个短横线之间,你可以使用 YAML 设置变量。你可以通过 fetch 函数访问它们。转换为 Markdown 是可选的。

如何使用

$page = new \Modularr\YAMLFrontmatter\FrontMatter('content/example.md');

echo '<h1><a href="'.$page->fetch('uri').'">'.$page->fetch('title').'</a></h1>
'.$page->fetch('content');

foreach($page->fetch('list') as $key => $value) {
    echo '<li>'.$key.' => '.$value.'</li>';
}
$array = $page->fetch('list');
echo $array['foo'];
---
foo: bar
title: Test
info: you can have as many custom fields as you like
date: 2005-09-16 17:20:42+00:00
layout: post
comments: true
slug: testing
list: { foo: bar, bar: baz }
list2:
- foo
- bar
list3:
    foo: bar
    bar: baz
---
<h1>Text Here</h1>
<p>content</p>

安装

通过 Composer 安装

composer require modularr/yaml-front-matter

如果你在一个 VanillaJS 项目中使用此包,请确保你有

require_once 'vendor/autoload.php';