3.0.5 2020-12-08 21:25 UTC

This package is auto-updated.

Last update: 2021-10-08 23:07:25 UTC


travis-badge Latest Stable Version License


Html is a simple package for building snippets of valid HTML. It can be used to augment a templating system where you want to encapsulate more logic in a class vs a template file.

Getting Started


This package requires PHP 7 or higher.


Via Composer:

composer require aviator/html



composer test



To build a block of HTML, make a new tag:

$tag = new Tag('div');

Or, using the static constructor:

$tag = Tag::make('div');

Or, using magic static calls:

$tag = Tag::div();

Or, using the global helper function:

$tag = tag('div');

To render the tag, call render():

echo $tag->render();

Which produces:


The Tag class will only accept valid HTML tags. Trying to create an invalid tag will throw an exception.


Tags can have contents. You can pass in a string, a Tag, or an array of either or both.


Use the with() method to add content to a tag:

$tag = tag('div')->with('some content');

This will render as:

<div>some content</div>

Additionally, with() will attempt to get a string representation of any non-string, non-renderable passed in, including objects that implement __toString().

Nested Tag

Tags can be nested:

$tag = tag('div')->with(
    tag('p')->with('some content')


<div><p>some content</p></div>


You can also use an array:

$tag = tag('ul')->with([
    tag('li')->with('list item 1'),
    tag('li')->with('list item 2'),
    'misplaced text',

Which will render:

  <li>list item 1</li>
  <li>list item 2</li>
  misplaced text

Void tags

The Tag class knows which tags are void and need no closing tag. There's no need to do anything for <input>, <hr>, etc.

Void tags cannot have content. Trying to add content to them will throw an exception.


Direct siblings can be rendered using a Fragment. A fragment is simply an un-nested collection of tags (or other renderables, even other fragments):

$fragment = new Fragment([
    new Tag('p'),
    new Tag('div'),

// Or Fragment::make([...]);



Conditional Tags

You can create a conditional tag that will only render sometimes:

$tag = new Tag('div');

// Evaluates equality, eg truthy and falsey values


$tag = Tag::when(false, 'div');



CSS Classes

To specify CSS classes for your tags, pass in a second parameter:

$tag = tag('div', 'some-class');


<div class="some-class"></div>

Multiple CSS classes may be passed in via array:

$tag = tag('div', ['class-one', 'class-two'])


<div class="class-one class-two"></div>

After instantiation

If you need to add classes after instantiation, you can call addClass(), which accepts the same string or array as the constructor:

$tag = tag('div');

$tag->addClass(['class2', 'class3']);


Attributes are passed in as the third parameter. Attributes with values are passed by association. Boolean attributes are simply a value.

$tag = tag('input', 'some-class', [
    'value' => 'content',


<input value="content" disabled>

After instantiation

If you need to add attributes after instantiation, you can call addAttribute(), which accepts the same array as the constructor:

$tag = tag('input');

$tag->addAttribute(['autocomplete' => 'off']);


Attributes are validated to make sure they belong to the tag you've applied them to. For instance adding the max attribute to a <div> will throw an exception.

Getting attribute values

If you want to retrieve an attribute from a Tag instance, call attribute($name). If your attribute exists you'll get the value (boolean attributes always return true), otherwise you'll get null.

echo tag('input')->addAttribute(['name' => 'some_name'])->attribute('name');

// Result: 'some_name'

echo tag('input')->addAttribute(['disabled'])->attribute('disabled');

// Result: true

echo tag('input')->attribute('foo');

// Result: null


If you want to render an open tag, call the dontClose() method:

echo tag('div')->dontClose()->render()