HTML
class HTML (View source)
A class that serves as a fluent interface to write HTML in PHP. It also helps with creating HTML elements on the fly.
Example:
// return an HTML element by using some tag name as a static method
// "div" can be replaced by any other valid HTML element tag name
$html = HTML::div('This is a div!', ['class' => 'container']);
// create elements using "->element()" or "->{$tagName}()"
// create entities using "->entity()"
// create comments using "->comment()"
// structure deeply nested elements using "->open()" and "->close()" (wrapping methods)
// make the next action conditional using "->condition()" or "->if()"
// execute some logic (loops, complex if-statements) while creating the HTML using "->function()" or "->do()"
// retrieve the final generated HTML by using "->render()"
(new HTML())
->element('h1', 'HTML Forms', ['class' => 'title'])
->open('form', ['method' => 'POST'])
->comment('SIMPLE FORM')
->h2('Example', ['class' => 'subtitle'])
->p('This is an example form.')
->br()
->if(isset($variable))->div('$variable is set')
->open('fieldset')
->legend('Form 1', ['style' => 'color: #333;'])
->label('Message: ', ['class' => 'text'])
->input(['type' => 'text', 'required'])
->entity('nbsp')
->input(['type' => 'submit', 'value' => 'Submit'])
->close()
->open('ul', ['class' => 'errors'])
->do(function () {
foreach (['Error 1', 'Error 2', 'Error 3'] as $error) {
$this->li($error);
}
})
->close()
->close()
->render();
Constants
TAGS |
HTML tags. |
protected TAG_CLOSING |
Closing tag. |
protected TAG_NORMAL |
Normal tag. |
protected TAG_OPENING |
Opening tag. |
Properties
protected array | $buffer | Current buffer. |
|
protected array | $conditions | Conditions track. |
|
protected array | $stack | Current stack (nesting). |
Methods
Makes HTML tags available as methods on the class.
Makes HTML tags available as static methods on the class.
HTML constructor.
Returns the current HTML buffer.
Defines a hyperlink.
Defines an abbreviation or an acronym.
Not supported in HTML5. Use <abbr> instead. Defines an acronym.
Defines contact information for the author/owner of a document.
Not supported in HTML5. Use <embed> or <object> instead. Defines an embedded applet.
Defines an area inside an image map.
Defines an article.
Defines content aside from the page content.
Checks if the passed condition and throws exception if it's not truthy.
Defines embedded sound content.
Defines bold text.
Specifies the base URL/target for all relative URLs in a document.
Not supported in HTML5. Use CSS instead. Specifies a default color, size, and font for all text in a document.
Isolates a part of text that might be formatted in a different direction from other text outside it.
Overrides the current text direction.
Not supported in HTML5. Use CSS instead. Defines big text.
Defines a section that is quoted from another source.
Defines the document's body.
Defines a single line break.
Defines a clickable button.
Used to draw graphics, on the fly, via scripting (usually JavaScript).
Defines a table caption.
Not supported in HTML5. Use CSS instead. Defines centered text.
Defines the title of a work.
Defines a piece of computer code.
Specifies column properties for each column within a <colgroup> element.
Specifies a group of one or more columns in a table for formatting.
Creates an HTML comment from the specified text and pass it to the buffer.
Takes a condition (some boolean value) to determine whether or not to create the very next element and pass it to the buffer.
Adds a machine-readable translation of a given content.
Specifies a list of pre-defined options for input controls.
Defines a description/value of a term in a description list.
Defines text that has been deleted from a document.
Defines additional details that the user can view or hide.
Specifies a term that is going to be defined within the content.
Defines a dialog box or window.
Not supported in HTML5. Use <ul> instead. Defines a directory list.
Defines a section in a document.
Defines a description list.
Alias for self::function()
. See the corresponding method for more details.
Defines a term/name in a description list.
Creates a complete HTML element (opening and closing tags) constructed from the specified parameters and pass it to the buffer.
Defines emphasized text.
Defines a container for an external application.
Groups related elements in a form.
Defines a caption for a <figure> element.
Specifies self-contained content.
Not supported in HTML5. Use CSS instead. Defines font, color, and size for text.
Defines a footer for a document or section.
Defines an HTML form for user input.
Not supported in HTML5. Defines a window (a frame) in a frameset.
Not supported in HTML5. Defines a set of frames.
Takes a callback and executes it after binding $this (HTML) to it, useful for example to execute any PHP code while creating the markup.
Defines HTML heading.
Defines HTML heading.
Defines HTML heading.
Defines HTML heading.
Defines HTML heading.
Defines HTML heading.
Contains metadata/information for the document.
Defines a header for a document or section.
Defines a thematic change in the content.
Defines the root of an HTML document.
Defines a part of text in an alternate voice or mood.
Alias for self::condition()
. See the corresponding method for more details.
Defines an inline frame.
Defines an image.
Defines an input control.
Defines a text that has been inserted into a document.
Replaces variables in the passed string with values from the passed variables.
Determines whether or not the last set condition is truthy or falsy.
Defines keyboard input.
Defines a label for an <input> element.
Defines a caption for a <fieldset> element.
Defines a list item.
Defines the relationship between a document and an external resource (most used to link to style sheets).
Specifies the main content of a document.
Defines an image map.
Defines marked/highlighted text.
Defines metadata about an HTML document.
Defines a scalar measurement within a known range (a gauge).
Minifies HTML buffers by removing all unnecessary whitespaces and comments.
Defines navigation links.
Creates an arbitrary text-node from the specified text and pass it to the buffer (useful to add some special tags, "<!DOCTYPE html>" for example).
Not supported in HTML5. Defines an alternate content for users that do not support frames.
Returns a normalized HTML tag name from the specified name.
Defines an alternate content for users that do not support client-side scripts.
Defines a container for an external application.
Defines an ordered list.
Creates an HTML opening tag from the specified parameters and pass it to the buffer. Works in conjunction with self::close()
.
Defines a group of related options in a drop-down list.
Defines an option in a drop-down list.
Defines the result of a calculation.
Defines a paragraph.
Defines a parameter for an object.
Defines a container for multiple image resources.
Defines preformatted text.
Represents the progress of a task.
Defines a short quotation.
Returns the created HTML elements found in the buffer and empties it.
Defines what to show in browsers that do not support ruby annotations.
Defines an explanation/pronunciation of characters (for East Asian typography).
Defines a ruby annotation (for East Asian typography).
Defines text that is no longer correct.
Defines sample output from a computer program.
Defines a client-side script.
Defines a section in a document.
Defines a drop-down list.
Defines smaller text.
Defines multiple media resources for media elements (<video> and <audio>).
Defines a section in a document.
Not supported in HTML5. Use <del> or <s> instead. Defines strikethrough text.
Returns an HTML attributes string from an associative array of attributes.
Defines important text.
Defines style information for a document.
Defines subscripted text.
Defines a visible heading for a <details> element.
Defines superscripted text.
Defines a container for SVG graphics.
Defines a table.
Groups the body content in a table.
Defines a cell in a table.
Defines a container for content that should be hidden when the page loads.
Defines a multiline input control (text area).
Groups the footer content in a table.
Defines a header cell in a table.
Groups the header content in a table.
Defines a specific time (or datetime).
Defines a title for the document.
Defines a row in a table.
Defines text tracks for media elements (<video> and <audio>).
Not supported in HTML5. Use CSS instead. Defines teletype text.
Defines some text that is unarticulated and styled differently from normal text.
Defines an unordered list.
Asserts that the passed HTML is valid.
Defines a variable.
Defines embedded video content.
Defines a possible line-break.
Details
mixed
__call(string $name, array $arguments)
Makes HTML tags available as methods on the class.
static mixed
__callStatic(string $name, array $arguments)
Makes HTML tags available as static methods on the class.
__construct(bool $strict = false, bool $indent = true)
HTML constructor.
string
__toString()
Returns the current HTML buffer.
static string
a(mixed $content = '', array $attributes = [])
Defines a hyperlink.
static string
abbr(mixed $content = '', array $attributes = [])
Defines an abbreviation or an acronym.
static string
acronym(mixed $content = '', array $attributes = [])
Not supported in HTML5. Use <abbr> instead. Defines an acronym.
static string
address(mixed $content = '', array $attributes = [])
Defines contact information for the author/owner of a document.
static string
applet(mixed $content = '', array $attributes = [])
Not supported in HTML5. Use <embed> or <object> instead. Defines an embedded applet.
static string
area(array $attributes = [])
Defines an area inside an image map.
static string
article(mixed $content = '', array $attributes = [])
Defines an article.
static string
aside(mixed $content = '', array $attributes = [])
Defines content aside from the page content.
private void
assert(bool $condition, string $message)
Checks if the passed condition and throws exception if it's not truthy.
The message that is passed to this function should contain four %s
placeholders for the
class
, function
, file
and line
of the previous caller (offset 1 of the backtrace).
static string
audio(mixed $content = '', array $attributes = [])
Defines embedded sound content.
static string
b(mixed $content = '', array $attributes = [])
Defines bold text.
static string
base(array $attributes = [])
Specifies the base URL/target for all relative URLs in a document.
static string
basefont(mixed $content = '', array $attributes = [])
Not supported in HTML5. Use CSS instead. Specifies a default color, size, and font for all text in a document.
static string
bdi(mixed $content = '', array $attributes = [])
Isolates a part of text that might be formatted in a different direction from other text outside it.
static string
bdo(mixed $content = '', array $attributes = [])
Overrides the current text direction.
static string
big(mixed $content = '', array $attributes = [])
Not supported in HTML5. Use CSS instead. Defines big text.
static string
blockquote(mixed $content = '', array $attributes = [])
Defines a section that is quoted from another source.
static string
body(mixed $content = '', array $attributes = [])
Defines the document's body.
static string
br(array $attributes = [])
Defines a single line break.
static string
button(mixed $content = '', array $attributes = [])
Defines a clickable button.
static string
canvas(mixed $content = '', array $attributes = [])
Used to draw graphics, on the fly, via scripting (usually JavaScript).
static string
caption(mixed $content = '', array $attributes = [])
Defines a table caption.
static string
center(mixed $content = '', array $attributes = [])
Not supported in HTML5. Use CSS instead. Defines centered text.
static string
cite(mixed $content = '', array $attributes = [])
Defines the title of a work.
HTML
close()
Creates an HTML closing tag matching the last tag opened by self::open()
.
static string
code(mixed $content = '', array $attributes = [])
Defines a piece of computer code.
static string
col(array $attributes = [])
Specifies column properties for each column within a <colgroup> element.
static string
colgroup(mixed $content = '', array $attributes = [])
Specifies a group of one or more columns in a table for formatting.
HTML
comment(string $text)
Creates an HTML comment from the specified text and pass it to the buffer.
HTML
condition(mixed $condition)
Takes a condition (some boolean value) to determine whether or not to create the very next element and pass it to the buffer.
static string
data(mixed $content = '', array $attributes = [])
Adds a machine-readable translation of a given content.
static string
datalist(mixed $content = '', array $attributes = [])
Specifies a list of pre-defined options for input controls.
static string
dd(mixed $content = '', array $attributes = [])
Defines a description/value of a term in a description list.
static string
del(mixed $content = '', array $attributes = [])
Defines text that has been deleted from a document.
static string
details(mixed $content = '', array $attributes = [])
Defines additional details that the user can view or hide.
static string
dfn(mixed $content = '', array $attributes = [])
Specifies a term that is going to be defined within the content.
static string
dialog(mixed $content = '', array $attributes = [])
Defines a dialog box or window.
static string
dir(mixed $content = '', array $attributes = [])
Not supported in HTML5. Use <ul> instead. Defines a directory list.
static string
div(mixed $content = '', array $attributes = [])
Defines a section in a document.
static string
dl(mixed $content = '', array $attributes = [])
Defines a description list.
HTML
do(callable $callback)
Alias for self::function()
. See the corresponding method for more details.
static string
dt(mixed $content = '', array $attributes = [])
Defines a term/name in a description list.
HTML
element(string $name, mixed $content = '', array $attributes = [])
Creates a complete HTML element (opening and closing tags) constructed from the specified parameters and pass it to the buffer.
static string
em(mixed $content = '', array $attributes = [])
Defines emphasized text.
static string
embed(array $attributes = [])
Defines a container for an external application.
HTML
entity(string $name)
Creates an HTML entity from the specified name and pass it to the buffer.
static string
fieldset(mixed $content = '', array $attributes = [])
Groups related elements in a form.
static string
figcaption(mixed $content = '', array $attributes = [])
Defines a caption for a <figure> element.
static string
figure(mixed $content = '', array $attributes = [])
Specifies self-contained content.
static string
font(mixed $content = '', array $attributes = [])
Not supported in HTML5. Use CSS instead. Defines font, color, and size for text.
static string
footer(mixed $content = '', array $attributes = [])
Defines a footer for a document or section.
static string
form(mixed $content = '', array $attributes = [])
Defines an HTML form for user input.
static string
frame(mixed $content = '', array $attributes = [])
Not supported in HTML5. Defines a window (a frame) in a frameset.
static string
frameset(mixed $content = '', array $attributes = [])
Not supported in HTML5. Defines a set of frames.
HTML
function(callable $callback)
Takes a callback and executes it after binding $this (HTML) to it, useful for example to execute any PHP code while creating the markup.
static string
h1(mixed $content = '', array $attributes = [])
Defines HTML heading.
static string
h2(mixed $content = '', array $attributes = [])
Defines HTML heading.
static string
h3(mixed $content = '', array $attributes = [])
Defines HTML heading.
static string
h4(mixed $content = '', array $attributes = [])
Defines HTML heading.
static string
h5(mixed $content = '', array $attributes = [])
Defines HTML heading.
static string
h6(mixed $content = '', array $attributes = [])
Defines HTML heading.
static string
head(mixed $content = '', array $attributes = [])
Contains metadata/information for the document.
static string
header(mixed $content = '', array $attributes = [])
Defines a header for a document or section.
static string
hr(array $attributes = [])
Defines a thematic change in the content.
static string
html(mixed $content = '', array $attributes = [])
Defines the root of an HTML document.
static string
i(mixed $content = '', array $attributes = [])
Defines a part of text in an alternate voice or mood.
HTML
if(mixed $condition)
Alias for self::condition()
. See the corresponding method for more details.
static string
iframe(mixed $content = '', array $attributes = [])
Defines an inline frame.
static string
img(array $attributes = [])
Defines an image.
static string
input(array $attributes = [])
Defines an input control.
static string
ins(mixed $content = '', array $attributes = [])
Defines a text that has been inserted into a document.
private string
interpolate(string $text, array $variables = [])
Replaces variables in the passed string with values from the passed variables.
protected bool
isConditionTruthy(int $depth = self::TAG_NORMAL)
Determines whether or not the last set condition is truthy or falsy.
static string
kbd(mixed $content = '', array $attributes = [])
Defines keyboard input.
static string
label(mixed $content = '', array $attributes = [])
Defines a label for an <input> element.
static string
legend(mixed $content = '', array $attributes = [])
Defines a caption for a <fieldset> element.
static string
li(mixed $content = '', array $attributes = [])
Defines a list item.
static string
link(array $attributes = [])
Defines the relationship between a document and an external resource (most used to link to style sheets).
static string
main(mixed $content = '', array $attributes = [])
Specifies the main content of a document.
static string
map(mixed $content = '', array $attributes = [])
Defines an image map.
static string
mark(mixed $content = '', array $attributes = [])
Defines marked/highlighted text.
static string
meta(array $attributes = [])
Defines metadata about an HTML document.
static string
meter(mixed $content = '', array $attributes = [])
Defines a scalar measurement within a known range (a gauge).
static string
minify(string $html)
Minifies HTML buffers by removing all unnecessary whitespaces and comments.
static string
nav(mixed $content = '', array $attributes = [])
Defines navigation links.
HTML
node(string $text)
Creates an arbitrary text-node from the specified text and pass it to the buffer (useful to add some special tags, "<!DOCTYPE html>" for example).
static string
noframes(mixed $content = '', array $attributes = [])
Not supported in HTML5. Defines an alternate content for users that do not support frames.
protected string
normalizeTagName(string $name)
Returns a normalized HTML tag name from the specified name.
static string
noscript(mixed $content = '', array $attributes = [])
Defines an alternate content for users that do not support client-side scripts.
static string
object(mixed $content = '', array $attributes = [])
Defines a container for an external application.
static string
ol(mixed $content = '', array $attributes = [])
Defines an ordered list.
HTML
open(string $name, array $attributes = [])
Creates an HTML opening tag from the specified parameters and pass it to the buffer. Works in conjunction with self::close()
.
static string
optgroup(mixed $content = '', array $attributes = [])
Defines a group of related options in a drop-down list.
static string
option(mixed $content = '', array $attributes = [])
Defines an option in a drop-down list.
static string
output(mixed $content = '', array $attributes = [])
Defines the result of a calculation.
static string
p(mixed $content = '', array $attributes = [])
Defines a paragraph.
static string
param(array $attributes = [])
Defines a parameter for an object.
static string
picture(mixed $content = '', array $attributes = [])
Defines a container for multiple image resources.
static string
pre(mixed $content = '', array $attributes = [])
Defines preformatted text.
static string
progress(mixed $content = '', array $attributes = [])
Represents the progress of a task.
static string
q(mixed $content = '', array $attributes = [])
Defines a short quotation.
string
render()
Returns the created HTML elements found in the buffer and empties it.
static string
rp(mixed $content = '', array $attributes = [])
Defines what to show in browsers that do not support ruby annotations.
static string
rt(mixed $content = '', array $attributes = [])
Defines an explanation/pronunciation of characters (for East Asian typography).
static string
ruby(mixed $content = '', array $attributes = [])
Defines a ruby annotation (for East Asian typography).
static string
s(mixed $content = '', array $attributes = [])
Defines text that is no longer correct.
static string
samp(mixed $content = '', array $attributes = [])
Defines sample output from a computer program.
static string
script(mixed $content = '', array $attributes = [])
Defines a client-side script.
static string
section(mixed $content = '', array $attributes = [])
Defines a section in a document.
static string
select(mixed $content = '', array $attributes = [])
Defines a drop-down list.
static string
small(mixed $content = '', array $attributes = [])
Defines smaller text.
static string
source(array $attributes = [])
Defines multiple media resources for media elements (<video> and <audio>).
static string
span(mixed $content = '', array $attributes = [])
Defines a section in a document.
static string
strike(mixed $content = '', array $attributes = [])
Not supported in HTML5. Use <del> or <s> instead. Defines strikethrough text.
protected string
stringifyAttributes(array $attributes)
Returns an HTML attributes string from an associative array of attributes.
static string
strong(mixed $content = '', array $attributes = [])
Defines important text.
static string
style(mixed $content = '', array $attributes = [])
Defines style information for a document.
static string
sub(mixed $content = '', array $attributes = [])
Defines subscripted text.
static string
summary(mixed $content = '', array $attributes = [])
Defines a visible heading for a <details> element.
static string
sup(mixed $content = '', array $attributes = [])
Defines superscripted text.
static string
svg(mixed $content = '', array $attributes = [])
Defines a container for SVG graphics.
static string
table(mixed $content = '', array $attributes = [])
Defines a table.
static string
tbody(mixed $content = '', array $attributes = [])
Groups the body content in a table.
static string
td(mixed $content = '', array $attributes = [])
Defines a cell in a table.
static string
template(mixed $content = '', array $attributes = [])
Defines a container for content that should be hidden when the page loads.
static string
textarea(mixed $content = '', array $attributes = [])
Defines a multiline input control (text area).
static string
tfoot(mixed $content = '', array $attributes = [])
Groups the footer content in a table.
static string
th(mixed $content = '', array $attributes = [])
Defines a header cell in a table.
static string
thead(mixed $content = '', array $attributes = [])
Groups the header content in a table.
static string
time(mixed $content = '', array $attributes = [])
Defines a specific time (or datetime).
static string
title(mixed $content = '', array $attributes = [])
Defines a title for the document.
static string
tr(mixed $content = '', array $attributes = [])
Defines a row in a table.
static string
track(array $attributes = [])
Defines text tracks for media elements (<video> and <audio>).
static string
tt(mixed $content = '', array $attributes = [])
Not supported in HTML5. Use CSS instead. Defines teletype text.
static string
u(mixed $content = '', array $attributes = [])
Defines some text that is unarticulated and styled differently from normal text.
static string
ul(mixed $content = '', array $attributes = [])
Defines an unordered list.
static void
validate(string $html)
Asserts that the passed HTML is valid.
static string
var(mixed $content = '', array $attributes = [])
Defines a variable.
static string
video(mixed $content = '', array $attributes = [])
Defines embedded video content.
static string
wbr(array $attributes = [])
Defines a possible line-break.
HTML
write(string $content)
Writes content to the HTML buffer.