italystrap / html
PHP中的HTML标签和属性生成器
1.2.0
2020-03-06 10:42 UTC
Requires
- php: >=7.2
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^0.5.0
- fig/link-util: ^1.1
- italystrap/debug: ~2.0
- lucatume/function-mocker-le: ^1.0
- lucatume/wp-browser: ~2.2
- phpcompatibility/php-compatibility: 9.3.*
- psr/link: ^1.0
- szepeviktor/phpstan-wordpress: ^0.6.0
- wp-coding-standards/wpcs: ^2.1
Suggests
- widoz/bem: BEM library - Take advantage of the bem syntax throught an object
README
PHP HTML处理对象化方法
目录
安装
使用此包的最佳方式是通过Composer
composer require italystrap/html
基本用法
属性类
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' );
属性也可以与同一命名空间下的get_attr()
和get_attr_e()
辅助函数一起使用。
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()
将属性列表构建成一个字符串,并在该字符串上应用上下文过滤器
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 );
或者
<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>' );
标签类
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' ] );
过滤器
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>
高级用法
更多高级用法请参考tests文件夹。
贡献
欢迎所有反馈/错误报告/拉取请求。
许可证
版权(c)2019 Enea Overclokk,ItalyStrap
本代码采用MIT许可证。
注意事项
- 在语义化版本控制指南下维护
鸣谢
待办事项