webuni/front-matter
Composer 安装命令:
composer require webuni/front-matter
包简介
Front matter parser and dumper for PHP
README 文档
README
The most universal Front matter (yaml, json, neon, toml) parser and dumper for PHP. Front matter allows page-specific variables to be included at the top of a page.
Features
This library can parse all form of front matter:
| YAML (Neon) | TOML | Json | Twig | Pug |
|---|---|---|---|---|
--- foo: bar --- # Section Text |
+++ foo = bar +++ # Section Text |
{
"foo": "bar"
}
# Section
Text
|
{#---
foo: bar
---#}
<h1>Section</h1>
<p>{{ foo }}</p>
|
--
foo: bar
h1 Section
p= foo
|
Installation
This library can be installed via Composer:
composer require webuni/front-matter
Usage
Automatic front matter detection and parsing
The following code will automatically detect the above front matter types:
<?php $frontMatter = \Webuni\FrontMatter\FrontMatterChain::create(); $document = $frontMatter->parse($string); $data = $document->getData(); $content = $document->getContent();
Parse an arbitrary string
<?php $frontMatter = new \Webuni\FrontMatter\FrontMatter(); $document = $frontMatter->parse($string); $data = $document->getData(); $content = $document->getContent();
Check if a string has front matter
<?php $frontMatter = new \Webuni\FrontMatter\FrontMatter(); $hasFrontMatter = $frontMatter->exists($string);
Twig loader
If you want to store metadata about twig template, e.g.:
{#--- title: Hello world menu: main weight: 20 ---#} {% extend layout.html.twig %} {% block content %} Hello world! {% endblock %}
you can use FrontMatterLoader, that decorates another Twig loader:
$frontMatter = \Webuni\FrontMatter\Twig\TwigCommentFrontMatter::create(); $loader = new \Twig\Loader\FilesystemLoader(['path/to/templates']); $loader = new \Webuni\FrontMatter\Twig\FrontMatterLoader($frontMatter, $loader); $twig = new \Twig\Environment($loader); $content = $twig->render('template', []); // rendered the valid twig template without front matter
It is possible to inject front matter to Twig template as variables:
// … $converter = \Webuni\FrontMatter\Twig\DataToTwigConvertor::vars(); $loader = new \Webuni\FrontMatter\Twig\FrontMatterLoader($frontMatter, $loader, $converter); // …
Loaded Twig template has this code:
{% set title = "Hello world" %}
{% set menu = "main" %}
{% set weight = 20 %}
{% line 5 %}
{% extend layout.html.twig %}
{% block content %}
Hello world!
{% endblock %}
Available converters:
| Converter | Twig |
|---|---|
DataToTwigConvertor::nothing() |
|
DataToTwigConvertor::vars() |
{% set key1 = value1 %} |
DataToTwigConvertor::vars(false) |
{% set key1 = key1 is defined ? key1 : value1 %} |
DataToTwigConvertor::var('name') |
{% set name = {key1: value1, key2: value2} %} |
DataToTwigConvertor::var('name', false) |
{% set name = name is defined ? name : {key1: value1} %} |
Markdown
The most commonly used front matter is for markdown files:
--- layout: post title: I Love Markdown tags: - test - example --- # Hello World!
This library can be used with league/commonmark:
$frontMatter = new \Webuni\FrontMatter\FrontMatter(); $extension = new \Webuni\FrontMatter\Markdown\FrontMatterLeagueCommonMarkExtension($frontMatter); $converter = new \League\CommonMark\CommonMarkConverter([]); $converter->getEnvironment()->addExtension($extension); $html = $converter->convertToHtml('markdown'); // html without front matter
Alternatives
webuni/front-matter 适用场景与选型建议
webuni/front-matter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 274.87k 次下载、GitHub Stars 达 40, 最近一次更新时间为 2015 年 06 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「yaml」 「json」 「neon」 「toml」 「front-matter」 「commonmark」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webuni/front-matter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webuni/front-matter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webuni/front-matter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Load configuration files the easy way. This configuration loader supports PHP, INI, JSON, XML, YAML and NEON file extensions.
A loader that is able to load various types of configuration files
Easy translating of PHP applications
统计信息
- 总下载量: 274.87k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 42
- 点击次数: 24
- 依赖项目数: 14
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-23