aduh95 / html-generator
PHP的HTML生成器,实现了大部分jQuery DOM操作API
1.0.0
2016-09-25 21:40 UTC
Requires (Dev)
- phpdocumentor/phpdocumentor: ~2.0
- phpunit/phpunit: ~5.0
Suggests
- wa72/html-pretty-min: Minify or indent HTML documents
- wa72/htmlpagedom: To add some more jQuery implementation in PHP
This package is not auto-updated.
Last update: 2024-09-14 19:31:14 UTC
README
本项目旨在通过友好的PHP命令生成有效和XSS安全的HTML。你可以使用一些jQuery DOM操作方法,因为我发现PHP中缺少了一些。该项目基于PHP的DOM函数构建,尽管性能相当不错。目标是提高可读性(即使是完全不知道PHP的人也能理解),并使检测和避免XSS变得更容易。你不再需要使用?>
闭合标签了!
如果你认为这个库缺少某个功能或设计上有问题,请随时贡献或提出问题。
安装
最简单的方法:使用Composer
composer require aduh95/html-generator
然后,你需要在脚本的开头包含Composer的自动加载文件。
<?php require 'vendor/autoload.php'; ?>
如果你不使用Composer(实际上你应该使用),你可以在发布部分找到PHAR存档。
<?php require 'HTMLGenerator.phar'; ?>
你还可以克隆这个仓库,并包含遵循PSR-4的PHP类。
入门
我建议继承Document
类来包含你的常用HTML标签。一旦我有时间,我会在Wiki中添加示例。
以下是主要功能的概述
<?php require 'vendor/autoload.php'; $doc = new aduh95\HTMLGenerator\Document('My title', 'en'); // If you want PHP to render you HTML in a way a human can read it // Default is compressed $doc->getDOMDocument()->formatOutput = true; // Add attribute array-like $doc->getHead()->appendChild($doc->createElement('meta'))['charset'] = 'uft-8'; // Or add attribute jQuery-like $doc->getHead()->link()->attr('rel', 'icon')->attr(['type'=>'image/png', 'href'=>'/asset/icon.png']); $doc()->p()->text('<script>alert("no XSS!");</script>') // add XSS-protected text easily ()->p()->append() // add children to an element with a simple method call ()->b('You are looking for something, aren\'t you?') // Add text content ()->br() // Auto closing tags are handled ()->a( ['href'=>'http://google.fr/', 'alt'=>'Search the "web"'], // An other method to add attributes 'YES, YOU CAN FIND IT!' )->data('user-color', 'red') // as in jQuery, you can set a "data-" attribute ()->br() ()->smaller(['class'=>'alert alert-info'])->text('This link is sponsored.') () ()->p('I ♥ Rock\'n\'Roll!') ->attr('test', 4) ->data('HTMLCamelCaseDataInformation', 'valid') // Transform CamelCase dataset to snake_case to match W3C standard ; // List shortcut $list = $doc()->ul(); $list[] = $doc->createTextNode('First <item>'); $list[] = $doc->createElement('b')->text('second one'); $list->append('third one'); $list->append() ()->li('fourth one'); // Table shortcut $table = $doc()->table(); $table[] = ['This', 'is', 'a', 'row']; $table[] = [$doc->createElement('td')->attr('rowspan', 3)->text('another'), 'one']; $table ->append(['data', 'in', 'the', 'row']) ->append([['multi', 'row'], ['so', 'easy']]); // This line is optionnal, the document will be automatically output at the end of ths script echo $doc; ?>
这将输出
<!DOCTYPE html> <html lang="en"> <head> <title>My title</title> <meta charset="uft-8"> <link rel="icon" type="image/png" href="/asset/icon.png"> </head> <body> <p><script>alert("XSS!");</script></p> <p><b>You are looking for something, aren't you?</b><br><a href="http://google.fr/" alt='Search the "web"' data-user-col or="red">YES, YOU CAN FIND IT!</a><br><smaller class="alert alert-info">This link is sponsored.</smaller></p> <p test="4" data-html-camel-case-data-information="valid">I ♥ Rock'n'Roll!</p> <ul> <li>First <item></li> <li><b>second one</b></li> <li>third one</li> <li>fourth one</li> </ul> <table><tbody> <tr> <td>This</td> <td>is</td> <td>a</td> <td>row</td> </tr> <tr> <td rowspan="3">another</td> <td>one</td> </tr> <tr> <td>data</td> <td>in</td> <td>the</td> <td>row</td> </tr> <tr> <td>multi</td> <td>row</td> </tr> <tr> <td>so</td> <td>easy</td> </tr> </tbody></table> </body> </html>