legiaifenix / json-element-parser
一个小型Json文件解析器,用于PHP,以便在视图中操作元素
0.0.1
2020-04-16 15:08 UTC
Requires
- php: >=7.1.0
This package is not auto-updated.
Last update: 2024-09-27 12:37:29 UTC
README
元素解析器
元素解析器是一个小型PHP包,可以读取文件并尝试将其结构输出到HTML。
这个想法源于需要快速部署一个允许我传递包含内容的文件(在我的案例中是条款和条件),这样的文件可以被快速翻译的需求。手中的项目没有仪表板,我认为返回一个包含可翻译条款和条件的数组的完整PHP文件不是最佳选择。
虽然拥有已经翻译的特定语言文件更容易,并且可以轻松地发送给任何人进行翻译,然后轻松地将它们附加到服务器上,而不必推送代码进行更改。
安装
composer require legiaifenix/json-element-parser
支持的文件类型
目前仅支持JSON
文件。根据必要性/流行度,可以实施更多,尽管优先级可能转向允许特定的样式、类和ID一致性。
用法
-
首先确定要使用的
JSON
文件路径和文件(例如:public/files/drawables/en.json
); -
调用
factory
并传递路径use legiaifenix\jsonParser\Factories\ElementsBuilderFactory; ... $builder = ElementsBuilderFactory::create($filePath);
-
您可以在任何时候要求构建器根据您的文件进行构建。当调用
build()
时,构建器会使用文件的全部内容,并遍历它以了解应绘制哪些类型的元素。$builder->build();
-
通过调用来绘制全部内容作为
HTML
$builder->draw();
异常
该包可以绘制哪些HTML内容?
标题
{ "title": "My first test" }
输出
<h1>My first test</h1>
段落
{
"Not informing a type defaults into paragraphs"
}
输出
<p>Not informing a type defaults into paragraphs</p>
逻辑结构
[ "My first paragraph!", { "title": "My main title", "content": [ "This is the first paragraph of my main title", "This follows the first paragraph as its second", "Can carry on adding paragraphs" ] } ]
输出
<p>My first paragraph!</p> <h1>My main title</h1> <p>This is the first paragraph of my main title</p> <p>This follows the first paragraph as its second</p> <p>Can carry on adding paragraphs</p>