tobento / service-tag
轻松创建带属性的HTML标签。
1.0.5
2022-10-28 05:57 UTC
Requires
- php: >=8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
- vimeo/psalm: ^4.0
README
PHP应用程序的HTML标签。
目录
入门
使用以下命令添加正在运行的标签服务项目的最新版本。
composer require tobento/service-tag
需求
- PHP 8.0或更高版本
亮点
- 框架无关,可与任何项目一起使用
- 解耦设计
文档
标签
创建标签
use Tobento\Service\Tag\Tag; use Tobento\Service\Tag\TagInterface; $tag = new Tag( name: 'p', html: 'html' // Must be escaped ); var_dump($tag instanceof TagInterface); // bool(true)
您可能需要调整默认参数。
use Tobento\Service\Tag\Tag; use Tobento\Service\Tag\Attributes; $tag = new Tag( name: 'li', html: 'html', // Must be escaped attributes: new Attributes(), // is default level: 2, // default is null renderEmptyTag: false, // default is true );
标签工厂
使用提供的标签工厂轻松创建标签
use Tobento\Service\Tag\TagFactory; use Tobento\Service\Tag\TagFactoryInterface; $tagFactory = new TagFactory(); var_dump($tagFactory instanceof TagFactoryInterface); // bool(true)
createTag
use Tobento\Service\Tag\TagFactory; use Tobento\Service\Tag\TagInterface; use Tobento\Service\Tag\Attributes; $tag = (new TagFactory())->createTag( name: 'p', html: '', // is default attributes: new Attributes(), // is default level: 2, // default is null ); var_dump($tag instanceof TagInterface); // bool(true)
createTagFromHtml
从指定的HTML创建标签。
use Tobento\Service\Tag\TagFactory; use Tobento\Service\Tag\TagInterface; $tag = (new TagFactory())->createTagFromHtml(html: 'html'); var_dump($tag instanceof TagInterface); // bool(true)
标签接口
获取器
use Tobento\Service\Tag\Tag; use Tobento\Service\Tag\TagInterface; use Tobento\Service\Tag\AttributesInterface; $tag = new Tag(name: 'li', html: 'html'); var_dump($tag instanceof TagInterface); // bool(true) var_dump($tag->getName()); // string(2) "li" var_dump($tag->getHtml()); // string(4) "html" var_dump($tag->getLevel()); // NULL (or int) var_dump($tag->attributes() instanceof AttributesInterface); // bool(true)
带有方法
with方法将返回一个新的实例。
use Tobento\Service\Tag\Tag; use Tobento\Service\Tag\Attributes; $tag = new Tag(name: 'li', html: 'html'); $tag = $tag->withName('ul'); $tag = $tag->withHtml('html'); $tag = $tag->withLevel(2); $tag = $tag->withAttributes(new Attributes()); $tag = $tag->withAttr(name: 'data-foo', value: 'bar');
操作方法
use Tobento\Service\Tag\Tag; $tag = new Tag(name: 'li', html: 'html'); $tag->prepend(html: 'foo'); var_dump($tag->getHtml()); string(7) "foohtml" $tag->append(html: 'bar'); var_dump($tag->getHtml()); // string(10) "foohtmlbar"
属性方法
attr方法将用相同名称覆盖属性。
use Tobento\Service\Tag\Tag; $tag = new Tag(name: 'li', html: 'html'); $tag->attr(name: 'data-foo', value: 'bar'); $tag->attr(name: 'data-foo', value: 'foo'); var_dump(htmlspecialchars((string)$tag)); // string(50) "<li data-foo="foo">html</li>"
use Tobento\Service\Tag\Tag; $tag = new Tag(name: 'li', html: 'html'); $tag->class(value: 'bar'); $tag->class(value: 'foo'); var_dump(htmlspecialchars((string)$tag)); // string(51) "<li class="bar foo">html</li>"
渲染方法
use Tobento\Service\Tag\Tag; $tag = new Tag(name: 'p', html: 'html'); <?= $tag->render() ?> // or just <?= $tag ?>
有时,如果您将渲染空标签设置为false,则可能有用,如果没有HTML,它返回一个空字符串。
use Tobento\Service\Tag\Tag; $tag = new Tag( name: 'p', html: '', renderEmptyTag: false ); var_dump($tag->render()); // string(0) ""
您可能希望自己处理开启和关闭。
use Tobento\Service\Tag\Tag; $tag = new Tag(name: 'p', html: 'html'); var_dump(htmlspecialchars($tag->open())); // string(9) "<p>" if (!$tag->isSelfClosing()) { var_dump(htmlspecialchars($tag->close())); // string(10) "</p>" }
NullTag
NullTag::class完全不渲染任何HTML标签,只渲染其HTML。
use Tobento\Service\Tag\NullTag; use Tobento\Service\Tag\TagInterface; $tag = new NullTag( html: 'html', // Must be escaped level: 2, // default is null ); var_dump($tag instanceof TagInterface); // bool(true) var_dump((string)$tag); // string(4) "html"
可标记
use Tobento\Service\Tag\Taggable; use Tobento\Service\Tag\HasTag; use Tobento\Service\Tag\Tag; use Tobento\Service\Tag\TagInterface; class Foo implements Taggable { use HasTag; } $foo = new Foo(); $foo->setTag(new Tag(name: 'p')); var_dump($foo->tag() instanceof TagInterface); // bool(true)
属性
创建属性
use Tobento\Service\Tag\Attributes; use Tobento\Service\Tag\AttributesInterface; $attributes = new Attributes(); var_dump($attributes instanceof AttributesInterface); // bool(true) // or $attributes = new Attributes([ 'class' => 'name', 'data-foo' => '', // set null as value or an int key // as only to render the name: 'required' => null, 1 => 'readonly', // turns into json string: 'data-bar' => ['key' => 'value'], ]); var_dump((string)$attributes); // string(90) " class="name" data-foo="" required readonly data-bar='{"key":"value"}'"
属性接口
empty
use Tobento\Service\Tag\Attributes; use Tobento\Service\Tag\AttributesInterface; $attributes = new Attributes(); var_dump($attributes instanceof AttributesInterface); // bool(true) var_dump($attributes->empty()); // bool(true) $attributes = new Attributes(['class' => 'foo']); var_dump($attributes->empty()); // bool(false)
has
use Tobento\Service\Tag\Attributes; $attributes = new Attributes(['class' => 'foo']); var_dump($attributes->has('id')); // bool(false) var_dump($attributes->has('class')); // bool(true)
get
use Tobento\Service\Tag\Attributes; $attributes = new Attributes([ 'class' => 'foo', 'data-foo' => ['key' => 'value'], ]); var_dump($attributes->get(name: 'id')); // NULL var_dump($attributes->get('class')); // string(3) "foo" var_dump($attributes->get('data-foo')); // array(1) { ["key"]=> string(5) "value" }
set
set方法将覆盖现有属性。
use Tobento\Service\Tag\Attributes; $attributes = new Attributes(); $attributes->set(name: 'class', value: 'foo'); $attributes->set('class', 'bar'); var_dump((string)$attributes); // string(12) " class="bar""
您可能只需为某些属性设置名称。
use Tobento\Service\Tag\Attributes; $attributes = new Attributes(); $attributes->set('readonly'); var_dump((string)$attributes); // string(9) " readonly"
add
add方法将“合并”现有属性。
use Tobento\Service\Tag\Attributes; $attributes = new Attributes(); $attributes->add(name: 'class', value: 'foo'); $attributes->add('class', 'bar'); var_dump((string)$attributes); // string(16) " class="foo bar"" $attributes = new Attributes(); $attributes->add('data-cars', ['volvo' => 'Volvo']); $attributes->add('data-cars', ['bmw' => 'BMW']); var_dump((string)$attributes); // string(82) " data-cars='{"volvo":"Volvo","bmw":"BMW"}'"
merge
merge方法将指定的属性与现有属性合并。
use Tobento\Service\Tag\Attributes; $attributes = new Attributes([ 'class' => 'foo', 'data-colors' => ['blue' => 'Blue'], ]); $attributes->merge(attributes: [ 'class' => 'bar', 'data-colors' => ['red' => 'Red'], ]); var_dump((string)$attributes); // string(98) " class="foo bar" data-colors='{"blue":"Blue","red":"Red"}'"
all
返回所有属性。
use Tobento\Service\Tag\Attributes; $attributes = new Attributes([ 'class' => 'foo', ]); var_dump($attributes->all()); // array(1) { ["class"]=> string(3) "foo" }
render
use Tobento\Service\Tag\Attributes; $attributes = new Attributes([ 'class' => 'foo', ]); var_dump($attributes->render()); // string(12) " class="foo""
如果有属性,它将在开头返回一个空格,如果没有,则返回一个空字符串。
这样您可以简单地做以下操作
<p<?= $attributes ?>>Lorem ipsum</p>
renderWithoutSpace
use Tobento\Service\Tag\Attributes; $attributes = new Attributes([ 'class' => 'foo', ]); var_dump($attributes->renderWithoutSpace()); // string(11) "class="foo""
Str
esc
返回转义后的字符串。
use Tobento\Service\Tag\Str; $escapedString = Str::esc( string: 'string', // string|Stringable flags: ENT_QUOTES, // default encoding: 'UTF-8', // default double_encode: true // default ); var_dump($escapedString); // string(6) "string"
formatTagAttributes
返回格式化并转义后的属性字符串。
use Tobento\Service\Tag\Str; $string = Str::formatTagAttributes( attributes: [ 'class' => 'bar', 'data-colors' => ['red' => 'Red'], ], withSpace: true, // default ); var_dump($string); // string(60) " class="bar" data-colors='{"red":"Red"}'"