It's just a simple class to create tags.
$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.
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=""foo" 'bar' <script></script>"></div>
$tag->getAttr('foo'); // "foo" 'bar' <script></script>
Get the attribute value.
True if the attribute exists. Even if it's empty.
Delete the attribute. Chainable.
Get the attributes in string.
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>
Replaces the content. Chainable.
True if there is some content.
Delete the content. Chainable.
Get the content string.
Helper methods for classes.
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>
Replace existing classes. Chainable. You can use it with the class pseudo method via __call:
$tag = Tag('img');
$tag->class('test); // <img class="test" />
Get the classes.
True when the class is set.
Delete one class. Chainable. Deletes the class attribute when you delete the last class.
Helper methods for data attributes.
Set the data attribute. Chainable.
$tag = new Tag('div');
$tag->data('foo', 'bar'); // <div data-foo="bar"></div>
Get the data value. Null if the data is not set.
True if there is a data attribute with this name, even if it's empty.
Delete the attribute.
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:
And there are some useful helper factories:
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>
You can add string or even an other tag:
$tag = Tag::labelFor(Tag::foo()->id('bar'), 'foobar'); // <label for"bar">foobar</label>
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 hidden ( string $name, string $value )
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!'))
);