italystrap/html
Composer 安装命令:
composer require italystrap/html
包简介
HTML tag and attributes generator in PHP
README 文档
README
PHP HTML handler the OOP way
Table Of Contents
Installation
The best way to use this package is through Composer:
composer require italystrap/html
Basic Usage
Attributes Class
use ItalyStrap\HTML\AttributesInterface; $sut = new AttributesInterface(); $sut->add( 'context', [ 'class' => 'color-primary', 'id' => 'unique_id', ] ); // ' class="color-primary" id="unique_id"' echo $sut->render( 'context' ); $sut->add( 'another_context', [ 'class' => '', // This will be skipped because empty 'attr1' => null, // This will be skipped because null 'attr2' => false, // This will be skipped because false 'attr3' => 0, // This will be skipped because 0 is also false 'id' => 'unique_id', ] ); // ' id="unique_id"' echo $sut->render( 'another_context' );
Attributes can be also used with the get_attr() and get_attr_e() helpers functions under the same namespece.
use function ItalyStrap\HTML\{get_attr, get_attr_e}; // Return ' class="someClass"' $attr = get_attr( 'context', ['class' => 'someClass'] ); // Echo ' class="someClass"' get_attr_e( 'context', ['class' => 'someClass'] );
ItalyStrap\HTML\get_attr()
Build list of attributes into a string and apply contextual filter on string:
use function ItalyStrap\HTML\{get_attr, get_attr_e}; $attr = [ 'id' => 'unique_id', 'class' => 'some_class', ]; $output = get_attr( $context, $attr, false ); // id="unique_id" class="some_class" printf( '<span%s>Title</span>', $output );
or
<span<?php get_attr_e( $context, $attr, true ) ?>>Title</span>
// <span id="unique_id" class="some_class">Title</span>
use function ItalyStrap\HTML\{open_tag, close_tag, open_tag_e, close_tag_e}; \ItalyStrap\HTML\Tag::$is_debug = false; // If you don't want tu print debug comments $open = \ItalyStrap\HTML\open_tag( 'test', 'div', [ 'class' => 'btn-primary' ] ); $this->assertStringContainsString( '<div class="btn-primary">', $open, '' ); $closed = \ItalyStrap\HTML\close_tag( 'test' ); $this->assertStringContainsString( '</div>', $closed, '' ); \ItalyStrap\HTML\Tag::$is_debug = false; \ItalyStrap\HTML\open_tag_e( 'test', 'div', [ 'class' => 'btn-primary' ] ); echo 'Content'; \ItalyStrap\HTML\close_tag_e( 'test' ); $this->expectOutputString( '<div class="btn-primary">Content</div>' );
Tag Class
use ItalyStrap\HTML\{Tag,AttributesInterface}; Tag::$is_debug = true; // This will print comment <! some comment> around the output for debugging, you can see it with ctrl + u key in the browser $sut = new Tag( new AttributesInterface() ); // <div class="someClass">Some content inside HTML div tags</div> echo $sut->open( 'some_context', 'div', [ 'class' => 'someClass' ] ); echo 'Some content inside HTML div tags'; echo $sut->close( 'some_context' ); // <input type="text"/> echo $sut->void( 'some_other_context', 'input', [ 'type' => 'text' ] );
Filters
use ItalyStrap\HTML\{Tag,AttributesInterface}; $context = 'some_context'; \add_filter("italystrap_{$context}_tag", function( string $tag, string $context, Tag $obj) { // Do some staff with $tag $new_tag = 'span'; return $new_tag; }, 10, 3); $sut = new Tag( new AttributesInterface() ); echo $sut->open( 'some_context', 'div', [ 'class' => 'someClass' ] ); echo 'Some content inside HTML div tags'; echo $sut->close( 'some_context' ); // <span class="someClass">Some content inside HTML div tags</span>
Advanced Usage
See in tests folder for more advance usage.
Contributing
All feedback / bug reports / pull requests are welcome.
License
Copyright (c) 2019 Enea Overclokk, ItalyStrap
This code is licensed under the MIT.
Notes
- Maintained under the Semantic Versioning Guide
Credits
TODO
italystrap/html 适用场景与选型建议
italystrap/html 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 907 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 italystrap/html 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 italystrap/html 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 907
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-12-09