, (*1)
Html
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., (*2)
Getting Started
Prerequisites
This package requires PHP 7 or higher., (*3)
Installing
Via Composer:, (*4)
composer require aviator/html
Testing
Run:, (*5)
composer test
Usage
To build a block of HTML, make a new tag:, (*6)
$tag = new Tag('div');
Or, using the static constructor:, (*7)
$tag = Tag::make('div');
Or, using magic static calls:, (*8)
$tag = Tag::div();
Or, using the global helper function:, (*9)
$tag = tag('div');
To render the tag, call render():, (*10)
echo $tag->render();
Which produces:, (*11)
<div></div>
The Tag class will only accept valid HTML tags. Trying to create an invalid tag will throw an exception., (*12)
Content
Tags can have contents. You can pass in a string, a Tag, or an array of either or both., (*13)
Strings
Use the with() method to add content to a tag:, (*14)
$tag = tag('div')->with('some content');
This will render as:, (*15)
<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()., (*16)
Nested Tag
Tags can be nested:, (*17)
$tag = tag('div')->with(
tag('p')->with('some content')
);
Render:, (*18)
<div><p>some content</p></div>
Array
You can also use an array:, (*19)
$tag = tag('ul')->with([
tag('li')->with('list item 1'),
tag('li')->with('list item 2'),
'misplaced text',
]);
Which will render:, (*20)
<ul>
<li>list item 1</li>
<li>list item 2</li>
misplaced text
</ul>
The Tag class knows which tags are void and need no closing tag. There's no need to do anything for <input>, <hr>, etc., (*21)
Void tags cannot have content. Trying to add content to them will throw an exception., (*22)
Fragments
Direct siblings can be rendered using a Fragment. A fragment is simply an un-nested collection of tags (or other renderables, even other fragments):, (*23)
$fragment = new Fragment([
new Tag('p'),
new Tag('div'),
]);
// Or Fragment::make([...]);
$fragment->render():, (*24)
<p></p><div></div>
You can create a conditional tag that will only render sometimes:, (*25)
$tag = new Tag('div');
// Evaluates equality, eg truthy and falsey values
$tag->setShouldRender(false);
or, (*26)
$tag = Tag::when(false, 'div');
$tag->render():, (*27)
''
CSS Classes
To specify CSS classes for your tags, pass in a second parameter:, (*28)
$tag = tag('div', 'some-class');
Render:, (*29)
<div class="some-class"></div>
Multiple CSS classes may be passed in via array:, (*30)
$tag = tag('div', ['class-one', 'class-two'])
Render:, (*31)
<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:, (*32)
$tag = tag('div');
$tag->addClass('some-class');
$tag->addClass(['class2', 'class3']);
Attributes
Attributes are passed in as the third parameter. Attributes with values are passed by association. Boolean attributes are simply a value., (*33)
$tag = tag('input', 'some-class', [
'value' => 'content',
'disabled'
]);
Render:, (*34)
<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:, (*35)
$tag = tag('input');
$tag->addAttribute(['autocomplete' => 'off']);
$tag->addAttribute(['disabled']);
Validation
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., (*36)
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., (*37)
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
Options
If you want to render an open tag, call the dontClose() method:, (*38)
echo tag('div')->dontClose()->render()
Result:, (*39)
<div>