serhii / goodbye-html
简单的HTML解析器,用于将HTML文件解析为字符串
Requires
- php: ^8.2
Requires (Dev)
- laravel/pint: dev-main
- phpstan/phpstan: 1.11.x-dev
- phpunit/phpunit: ^11.0@dev
- squizlabs/php_codesniffer: 4.0.x-dev
- symfony/var-dumper: ^5.0@dev
- dev-main
- v2.9.5.x-dev
- 2.9.5
- v2.9.4.x-dev
- 2.9.4
- v2.9.3.x-dev
- 2.9.3
- v2.9.2.x-dev
- 2.9.2
- v2.9.1.x-dev
- 2.9.1
- v2.9.0.x-dev
- 2.9.0
- v2.8.0.x-dev
- 2.8.0
- v2.7.0.x-dev
- 2.7.0
- v2.6.0.x-dev
- 2.6.0
- v2.5.0.x-dev
- 2.5.0
- v2.4.0.x-dev
- 2.4.0
- v2.3.0.x-dev
- 2.3.0
- v2.2.0.x-dev
- 2.2.0
- v2.1.0.x-dev
- 2.1.0
- v2.0.x-dev
- 2.0
- v1.6.x-dev
- v1.6.3.x-dev
- 1.6.3
- v1.6.2.x-dev
- 1.6.2
- 1.6.1
- 1.6
- v1.5.x-dev
- 1.5
- v1.4.1.x-dev
- 1.4.1
- v1.4
- 1.3
- 1.2.x-dev
- v1.1.x-dev
- v1.1
- v1.0
This package is auto-updated.
Last update: 2024-09-06 18:43:21 UTC
README
这是一个非常简单的包,用于将PHP逻辑从HTML或其他文本中分离出来。它允许您将 变量、if/elseif/else语句、循环 和 三元运算符 插入到任何文本文件中,并动态获取此文件的解析内容。
支持的PHP版本
- ✅ 8.2
- ✅ 8.3
它是用来做什么的?
当您需要将PHP逻辑从HTML或其他文本中分离出来时,此包非常有用。例如,如果您需要发送包含一些动态内容的电子邮件,您可以在包含HTML的模板文件中插入变量、if/elseif/else语句、循环和三元运算符。然后,您可以将此文件传递给解析器,以获取该文件的解析内容作为字符串。然后,您可以使用此字符串作为电子邮件的内容。
它不适用于什么?
此包不是用于创建功能齐全的模板引擎。它只是一个简单的解析器,允许您将一些PHP逻辑插入到任何文本文件中。它不是用于创建类似Twig、Blade或Latte的功能齐全的模板引擎。如果您需要功能齐全的模板引擎,应使用上述之一。
Goodbye HTML有什么?
- 变量
- 变量赋值
- 使用变量
- 打印变量
- 比较运算符
- 等于 (==)
- 不等于 (!=)
- 严格等于 (===)
- 严格不等于 (!==)
- 大于 (>)
- 小于 (<)
- 大于等于 (>=)
- 小于等于 (<=)
- if/Else-If/Else语句
- 表达式
- 三元表达式 (true ? 'yes' : 'no')
- 分组表达式 ((3 + 4) * 5)
- 循环
- 前缀运算符
- 否定运算符 (!)
- 减号运算符 (-)
- 字符串连接
- 数学运算
- 加法
- 减法
- 乘法
- 除法
- 求模
用法
use Serhii\GoodbyeHtml\Parser; $variables = [ 'title' => 'Title of the document', 'uses_php_3_years' => true, 'show_container' => false, ]; // Absolute file path to a text file $file_path = __DIR__ . '/hello.html'; $parser = new Parser($file_path, $variables); // this will parsed content of hello.html file echo $parser->parseHtml();
在解析之前包含2个PHP变量的HTML文件内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{{ $title }}</title> </head> <body class="{{ $show_container ? 'container' : '' }}"> <nav> <ul> {{ loop 1, 3 }} <li><a href="#">Link - {{ $index }}</a></li> {{ end }} </ul> </nav> {{ if $uses_php_3_years }} <h1>I'm not a pro, but it's only a matter of time</h1> {{ end }} </body> </html>
解析后的HTML到PHP字符串
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title of the document</title> </head> <body class=""> <nav> <ul> <li><a href="#">Link - 1</a></li> <li><a href="#">Link - 2</a></li> <li><a href="#">Link - 3</a></li> </ul> </nav> <h1>I'm not a pro, but it's only a matter of time</h1> </body> </html>
相同示例,但用于WordPress短代码
use Serhii\GoodbyeHtml\Parser; add_shortcode('my_shortcode', 'shortcode_callback'); function shortcode_callback() { $parser = new Parser(__DIR__ . '/shortcodes/main.html', [ 'title' => 'Title of the document', 'uses_php_3_years' => true, 'show_container' => false, ]); return $parser->parseHtml(); }
选项
Parser类的实例将第三个参数作为ParserOption枚举。您可以将它作为第三个参数传递给Parser类的构造函数。目前,它只有一个选项
ParserOption::PARSE_TEXT
如果您传递此选项,解析器将不会获取提供的文件路径的内容,而是解析提供的字符串。此选项在您想要解析字符串而不是文件时非常有用。
$parser = new Parser('<div>{{ $title }}</div>', [ 'title' => 'Hello world' ], ParserOption::PARSE_TEXT); // output: <div>Hello world</div>
支持的类型
您可以将其传递给解析器以包含在html/text文件中的类型。请注意,并非所有PHP类型都得到支持。在未来的版本中还将添加更多类型。
支持的前缀运算符
前缀运算符用于改变变量的值。例如,如果你有一个变量 $is_smoking,并且你想检查它是否为假,你可以使用前缀运算符 ! 来将变量的值更改为假。或者,如果你有一个变量 $age,并且你想使其变为负数,你可以使用前缀运算符 - 来将变量的值更改为负数。
支持的中缀运算符
中缀运算符用于执行数学运算或字符串连接。例如,如果你有一个变量 $age,并且你想将其加1,你可以使用中缀运算符 + 来给变量加1。或者,如果你有一个变量 $first_name,并且你想将其与 $last_name 连接起来,你可以使用中缀运算符 . 来连接这两个变量。
HTML/文本文件中所有可用的语法
变量
<!-- Inside html tags --> <div>{{ $guest_name }}</div>
<!-- Inside attributes --> <h2 class="{{ $styles }}">The title of the page</h2>
条件语句
<!-- Block syntax --> <section> {{ if true }} <h1>PHP is awesome programming language</h1> {{ end }} </section>
<!-- Inline syntax --> <h1 class="{{if $show_container}}container{{end}}"> This package is cool </h1>
如果/否则语句
<!-- Block syntax --> <section> {{ if $likes_bread }} <h1>I like bread</h1> {{ else }} <h1>I don't really like bread</h1> {{ end }} </section>
<!-- Inline syntax --> <section> <h1>{{ if $late }}It's late{{ else }}It's not late{{ end }}</h1> </section>
if/Else-If/Else语句
与PHP类似,你可以以相同的方式写入
elseif或else if。
<!-- Block syntax --> <section> {{ if $likes_bread }} <h1>I like bread</h1> {{ else if $likes_cake }} <h1>I like cake</h1> {{ elseif $likes_pizza }} <h1>I like pizza</h1> {{ else }} <h1>I don't really like anything</h1> {{ end }} </section>
<!-- Inline syntax --> <section> <h1>I like {{ if $likes_bread }}bread{{ else if $likes_cake }}cake{{ else }}just water{{ end }}</h1> </section>
三元运算符
三元运算符 通常被称为条件运算符,即内联的if/else。表达式 a ? b : c 在 a 的值为真时求值为 b,否则求值为 c。可以读作“如果a则b否则c”。
<!-- Inside html attributes --> <section class="{{ $wrap ? 'container' : '' }}"> <h1>Title</h1> </section>
<!-- With strings --> <section class="container"> {{ 23 === 23 ? '<h1>Main title</h1>' : '<h2>Secondary</h2>' }} </section>
<!-- With variables --> <section class="container"> {{ $has_apple ? $with_apple : $without_apple }} </section>
循环
循环需要两个整型参数。第一个参数是从哪个数字开始循环,第二个参数是停止循环的位置。例如,如果你从1开始到4,它将产生4个重复的块。在循环内部,你可以使用 $index 变量,它将具有当前迭代数字的值。
<!-- Block syntax --> <div> {{ loop 0, 5 }} <h1>Hello world {{ $index }}</h1> {{ end }} </div>
<!-- Inline syntax --> <div> <h1 class="{{ loop 1, 4 }}class-{{$index}} {{ end }}"></h1> </div>
<!-- With integer variables --> <div> {{ loop $from, $to }} <h1>Hello world {{ $index }}</h1> {{ end }} </div>
赋值语句
你可以在你的文本文件中使用花括号来给变量赋值。例如,如果你想将值5赋给变量 $a,你可以这样做 {{ $a = 5 }}。你也可以使用前缀运算符来更改变量的值。例如,如果你想将值false赋给变量 $is_smoking,你可以这样做 {{ $is_smoking = !true }}。
<div>{{ $age = 33 }}</div>
入门指南
$ composer require serhii/goodbye-html