alius/tag

创建HTML标签

1.0.6 2016-09-07 16:18 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:20:39 UTC


README

Build Status

Alius Tag

它只是一个用于创建标签的简单类。

基本用法

$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>

使用render方法或将类转换为字符串以获取HTML。

方法

属性

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

更改属性。可链式调用。

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

保留原始PHP类型

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

HTML实体和双引号将被转换(仅在渲染时)

$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 )

获取属性值。

bool hasAttr ( string $name )

如果存在属性则为true。即使它是空的。

object deleteAttr ( string $name )

删除属性。可链式调用。

string renderAttr ( string $name )

获取字符串形式的属性。

内容

object add ( mixed $value )

添加内容。可链式调用。值将被转换为字符串,null或空字符串将被忽略。

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

object setContent ( mixed $value )

替换内容。可链式调用。

bool hasContent

如果存在内容则为true。

object deleteContent

删除内容。可链式调用。

string renderContent

获取内容字符串。

类辅助方法。

object addClass ( $values )

添加类。可链式调用。null和空字符串将被忽略。您可以传递一个数组或字符串。无需担心空白符。

$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 )

替换现有类。可链式调用。您可以使用类伪方法 __call 来使用它。

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

array getClass ()

获取类。

bool hasClass ( $value )

当设置类时为true。

object deleteClass ( $values )

删除一个类。可链式调用。当删除最后一个类时,删除类属性。

数据

数据属性辅助方法。

object data ( $name, $value = null )

设置数据属性。可链式调用。

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

mixed getData ( $name )

获取数据值。如果没有设置数据,则为null。

bool hasData ( $name )

如果存在具有此名称的数据属性(即使它是空的),则为true。

object deleteData ( $name )

删除属性。

其他

不存在的方法定义为使用 __call 方法作为属性处理

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

您可以使用此类进行静态调用

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

存在一个通用工厂

object make ( string $tag )

还有一些有用的辅助工厂

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

如果表单不是get或post,我们将方法更改为post并添加一个_method隐藏标签

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

如果您设置了令牌,我们将放置一个_token隐藏标签

$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 )

您可以添加字符串或甚至另一个标签

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

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

与选项标签一起使用

$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 )
对象 radio ( 字符串 $name, 字符串 $value = null, $checked = false )
对象 text ( 字符串 $name, 字符串 $value = null )
对象 password ( 字符串 $name )
对象 hidden ( 字符串 $name, 字符串 $value )
对象 option ( 字符串 $text, 字符串 $value = null, $selected = false )
对象 textarea ( 字符串 $name, 字符串 $value = null )

你好,世界

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!'))
    );