定制 alius/tag 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alius/tag

Composer 安装命令:

composer require alius/tag

包简介

Create html tags

README 文档

README

Build Status

Alius Tag

It's just a simple class to create tags.

Basic usage

$img = new Tag('img');
print $img; // <img />

$img->src('url')
print $img; // <img src="url" />

$img->alt();
print $img; // <img src="url" alt="" />

$img->alt('Cute cat')
print $img; // <img src="url" alt="Cute cat" />

$img->class('cute');
print $img; // <img src="url" alt="Cute cat" class="cute" />

$img->class('dog');
print $img; // <img src="url" alt="Cute cat" class="dog" />

$img->addClass('cute');
print $img; // <img src="url" alt="Cute cat" class="dog cute" />

$div = new Tag('div');
$div->add($img);
print $div; // <div><img src="url" alt="Cute cat" class="dog cute" /></div>

$div->data('ng-class', 'test');
print $div; // <div data-ng-class="test"><img src="url" alt="Cute cat" class="dog cute" /></div>

Use the render method or cast the class to string to get the html.

Methods

Attributes

object attr ( string $name, mixed $value = null )

Change the attribute. Chainable.

$tag = new Tag('div');
$tag->attr('foo'); // <div foo=""></div>>
$tag->attr('foo', ''); // <div foo=""></div>
$tag->attr('foo', 'bar'); // <div foo="bar"></div>

Preserves the original php type:

$tag->attr('foo', true); // <div foo="1"></div>
$tag->getAttr('foo'); // true

Html entities and double quotes are converted (only when rendered):

$tag->attr('foo', '"foo" \'bar\' <script></script>'); // <div foo="&quot;foo&quot; 'bar' &lt;script&gt;&lt;/script&gt;"></div>
$tag->getAttr('foo'); // "foo" 'bar' <script></script>

mixed getAttr ( string $name )

Get the attribute value.

bool hasAttr ( string $name )

True if the attribute exists. Even if it's empty.

object deleteAttr ( string $name )

Delete the attribute. Chainable.

string renderAttr ( string $name )

Get the attributes in string.

Content

object add ( mixed $value )

Add content. Chainable. The value is converted to string, null or empty string is skipped.

$tag = new Tag('div');
$tag->add('foo')->add('bar'); // <div>foobar</div>

object setContent ( mixed $value )

Replaces the content. Chainable.

bool hasContent

True if there is some content.

object deleteContent

Delete the content. Chainable.

string renderContent

Get the content string.

Class

Helper methods for classes.

object addClass ( $values )

Add class(es). Chainable. Null and empty string skipped. You can pass an array or string. Don't worry about whitespaces.

$tag = new Tag('div');
$tag->addClass('foo')-addClass('bar'); // <div class="foo bar"></div>
$tag->addClass([' a ', ' b c ']); // <div class="foo bar a b c"></div>

object setClass ( $values )

Replace existing classes. Chainable. You can use it with the class pseudo method via __call:

$tag = Tag('img');
$tag->class('test); // <img class="test" />

array getClass ()

Get the classes.

bool hasClass ( $value )

True when the class is set.

object deleteClass ( $values )

Delete one class. Chainable. Deletes the class attribute when you delete the last class.

Data

Helper methods for data attributes.

object data ( $name, $value = null )

Set the data attribute. Chainable.

$tag = new Tag('div');
$tag->data('foo', 'bar'); // <div data-foo="bar"></div>

mixed getData ( $name )

Get the data value. Null if the data is not set.

bool hasData ( $name )

True if there is a data attribute with this name, even if it's empty.

object deleteData ( $name )

Delete the attribute.

Other

Non-existing methods are treated as attributes using the __call method:

$tag = new Tag('img');
$tag->foo('bar')->bar('foo'); // <img foo="bar" bar="foo" />

You can use this class statically:

$tag = Tag::anything(); // <anything></anything>

There is a general factory:

object make ( string $tag )

And there are some useful helper factories:

object form ( string $action, $method = 'post', $token = null )

If the form isn't get or post we change the method to post and add a _method hidden tag:

$tag = Tag::form('url', 'delete');
<form action="url" method="post">
    <input type="hidden" name="_method" value="delete" />
</form>

If you set the token we place a _token hidden tag:

$tag = Tag::form('url', 'post', 'foo bar');
<form action="url" method="post">
    <input type="hidden" name="_token" value="foo bar" />
</form>

object labelFor ( mixed $for, string $text = null )

You can add string or even an other tag:

$tag = Tag::labelFor(Tag::foo()->id('bar'), 'foobar'); // <label for"bar">foobar</label>

object select ( string $name, array $options = [] )

Use it with option tags:

$tag = Tag::select('foo', [
    Tag::option('one', 1),
    Tag::option('two', 2),
    Tag::option('three', 3),
]);
<select name="foo">
    <option value="1">one</option>
    <option value="2">two</option>
    <option value="3">three</option>
</select>
object div ( mixed $content = null )
object span ( mixed $content = null )
object a ( string $href, mixed $content = null )
object img ( string $src )
object caption ( mixed $text = null )
object input ( string $type, string $name, string $value = null )
object checkbox ( string $name, string $value = null, $checked = false )
object radio ( string $name, string $value = null, $checked = false )
object text ( string $name, string $value = null )
object password ( string $name )
object hidden ( string $name, string $value )
object option ( string $text, string $value = null, $selected = false )
object textarea ( string $name, string $value = null )

Hello World

print Tag::html()->lang('en')
    ->add(Tag::head()
        ->add(Tag::title()->add('HTML5'))
        ->add(Tag::meta()->charset('utf-8'))
        ->add(Tag::meta()->author('Romeo Vegvari'))
    )
    ->add(Tag::body()
        ->add(Tag::div('Hello World!'))
    );

alius/tag 适用场景与选型建议

alius/tag 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53.82k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 alius/tag 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 alius/tag 我们能提供哪些服务?
定制开发 / 二次开发

基于 alius/tag 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 53.82k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 10
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0
  • 更新时间: 2015-12-18