Download the PHP package aviator/html without Composer
On this page you can find all versions of the php package aviator/html. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package html
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.
Getting Started
Prerequisites
This package requires PHP 7 or higher.
Installing
Via Composer:
Testing
Run:
Usage
Tags
To build a block of HTML, make a new tag:
Or, using the static constructor:
Or, using magic static calls:
Or, using the global helper function:
To render the tag, call render()
:
Which produces:
The Tag
class will only accept valid HTML tags. Trying to create an invalid tag will throw an exception.
Content
Tags can have contents. You can pass in a string, a Tag
, or an array of either or both.
Strings
Use the with()
method to add content to a tag:
This will render as:
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:
Render:
Array
You can also use an array:
Which will render:
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.
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):
$fragment->render()
:
Conditional Tags
You can create a conditional tag that will only render sometimes:
or
$tag->render()
:
CSS Classes
To specify CSS classes for your tags, pass in a second parameter:
Render:
Multiple CSS classes may be passed in via array:
Render:
After instantiation
If you need to add classes after instantiation, you can call addClass()
, which accepts the same string or array as the constructor:
Attributes
Attributes are passed in as the third parameter. Attributes with values are passed by association. Boolean attributes are simply a value.
Render:
After instantiation
If you need to add attributes after instantiation, you can call addAttribute()
, which accepts the same array as the constructor:
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.
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.
Options
If you want to render an open tag, call the dontClose()
method:
Result: