buttress / phpx
PHP DOM 但更直观
v0.2
2024-03-12 04:53 UTC
Requires
- php: >8
- ext-dom: *
Requires (Dev)
- laravel/pint: ^1.14
- pestphp/pest: ^2
README
<PHPX/>
一个直观的DOMDocument包装器,使得在纯PHP中编写安全有效的HTML变得容易。
$x = new Buttress\Phpx\Phpx(); $github = 'https://github.com/buttress/phpx'; echo $x->render( $x->main(class: 'content', c: [ $x->h1(id: 'title', c: 'Hello World!'), $x->p(c: [ 'Brought to you by ', $x->a(href: $github, c: 'PHPX') ]), ]) );
变为
<main class="content"> <h1 id="title">Hello World!</h1> <p> Brought to you by <a href="https://github.com/buttress/phpx">PHPX</a> </p> </main>
安装
要安装PHPX,请使用composer
composer require phpx/phpx
用法
/** Basic Usage */ // <span>foo</span> $x->render($x->span(c: 'Foo')); // <a href='#'>foo</span> $x->render($x->a(href: '#', c: 'Foo')); // <div><span>Hello</span><strong>world</strong></div> $x->render($x->div(c: [ $x->span(c: 'Hello'), $x->strong(c: 'world'), ])); // Context specific XSS protection $xss = '\'"<>"'; // <span title="'"<>"">'"<>"</span> $x->render($x->span(title: $xss, c: $xss));
高级用法
/** * $x->if : Conditional */ // Renders either: <div>There are <strong>100</strong> items</div> // or <div>No items found</div> $total = 100; $result = $x->render( ...$x->if( $total > 0, // if fn() => $x->div(c: ['There are ', $x->strong(c: (string) $total), ' items']), // else fn() => $x->div(c: 'No items found.') ) ); /** * $x->foreach : Loop over a set of items */ $features = ['safe', 'valid', 'simple']; // <li>safe</li><li>valid</li><li>simple</li> $result = $x->render( ...$x->foreach($features, fn(string $feature) => $x->li(c: ucfirst($feature))) ); /** * $x->with : Capture a variable */ // Only run `getProduct()` when `$test` is true $result = $x->render( $x->div(c: [ ...$x->if($test, fn() => $x->with(getProduct(), fn(Product $product) => [ $x->h3(c: $product->title), $x->p(c: $product->description) ])) ]) ); /** * Mix HTML and PHPX */ ?> <div class='container'> <?= $x->render($x->h1(c: getProduct()->title)) ?> <span>The best product around</span> </div>
相关项目
- PHPX Compile PHPX的一个实验性编译器。显著减少函数调用。
- PHPX Templates 基于 PHPX 和 PHPX Compile 的实验性模板引擎。
贡献
欢迎为PHPX做出贡献!请随意fork存储库并提交一个pull request。
许可证
PHPX是在MIT许可证下发布的。
Git钩子
要添加我们的Git钩子并在提交前运行测试
git config --local core.hooksPath .githooks
支持
如果您遇到任何问题或有任何疑问,请在GitHub上创建一个问题。
感谢您检查PHPX ❤️