Utilities for programmatically generating consistent HTML
All code samples assume use \Webbower\HTML
at the top to allow for brevity in sample code. Otherwise, you'll need to prepend \Webbower\
to all of your calls.
Sets the profile for how you'd like your HTML to be generated
// Set HTML5 (non-XML) profile
HTML::setProfile(HTML::HTML5);
The HTML
class defaults to HTML::HTML5
if setProfile()
is never called.
Possible profiles are:
HTML::HTML5
- HTML 5 without XML restrictionsHTML::HTML5_XML
- HTML 5 with XML restrictionsHTML::XHTML11
- XHTML 1.1 rulesHTML::XHTML1_STRICT
- XHTML 1.0 Strict rulesHTML::XHTML1_TRANS
- XHTML 1.0 Transitional rulesHTML::HTML4_STRICT
- HTML 4.01 Strict rulesHTML::HTML4_TRANS
- HTML 4.01 Transitional rules
The profiles have the following effects:
HTML5
andHTML5_XML
allow for use of the HTML5 tags. If you are using a non-HTML5 profile, HTML5 tags will be downgraded todiv
andspan
for block and inline tags respectively, and append the original tag name to the class attributeHTML5_XML
,XHTML11
,XHTML1_STRICT
, andXHTML1_TRANS
all follow XML rules:- All tag names and attribute names are forced to lowercase
- All boolean attributes (like
disabled
,required
, etc) must have a value that duplicates the attribute name - All attribute values are quoted with double quotes
- All empty tags are self-closed with
/
(a space and a forward-slash)
- Conversely,
HTML5
,HTML4_STRICT
,HTML4_TRANS
are much looser on the requirements and will create the shortest possible tags:- Tag names will not have their case modified
- Boolean attributes can omit their attribute value
- Attribute values with no spaces can be unquoted
- Empty tags do not render with the self-closing string
Returns a string representing the current profile
Returns the appropriate DOCTYPE based on the currently set profile
HTML::setProfile(HTML::XHTML11);
echo HTML::doctype();
// Outputs <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
Constructs a valid HTML tag based on the profile
HTML::setProfile(HTML::HTML5);
echo HTML::tag('article', 'Lorem ipsum', array('id' => 'entry1', 'class' => 'entry'));
// Outputs <article id=entry1 class=entry>Lorem ipsum</article>
HTML::setProfile(HTML::HTML5_XML);
echo HTML::tag('article', 'Lorem ipsum', array('id' => 'entry1', 'class' => 'entry'));
// Outputs <article id="entry1" class="entry">Lorem ipsum</article>
HTML::setProfile(HTML::XHTML11);
echo HTML::tag('article', 'Lorem ipsum', array('id' => 'entry1', 'class' => 'entry'));
// Outputs <div id="entry1" class="entry article">Lorem ipsum</div>
$tagname
- (string) The tag name$content
- (string) The content for the tag. Ignored if the tag name is an empty tag$attrs
- (array) An array of attributes. See documentation forHTML::attrs()
for how to structure this array
Takes an associative array and converts it into a valid string to be used as the attributes for an HTML tag, based on the current profile.
HTML::setProfile(HTML::HTML5);
echo HTML::attrs(array('id' => 'entry1', 'class' => 'entry', 'disabled' => true, 'autofocus' => 'autofocus'));
// Outputs: id="entry1" class="entry" disabled autofocus
// Compared to an XML-based profile
HTML::setProfile(HTML::XHTML11);
echo HTML::attrs(array('id' => 'entry1', 'class' => 'entry', 'disabled' => true, 'autofocus' => 'autofocus'));
// Outputs: id="entry1" class="entry" disabled="disabled" autofocus="autofocus"
$attrs
- (array) An array of attributes. The key is the attribute name and the value is the attribute value. For boolean attributes, you can either have the key and value be the same, or set the value to a literaltrue
$tagname
- (string) The tag name$attrs
- (array) An array of attributes. See documentation forHTML::attrs()
for how to structure this array
Returns a complete and valid opening tag with attributes and is self-closed for empty tags in an XML-based profile
$tagname
- (string) The tag name
Returns a complete and valid closing tag, or an empty string if $tagname
is the name of an empty tag
Escapes all special HTML characters (<, >, ", ', and &)
This is basically a pass-through to the native PHP htmlspecialchars
function with a predefined set of configuration arguments based on current configuration. HTML::unescape()
is the inverse of this.
$str
- (string) The string to escape
Unescapes all special HTML characters (<, >, ", ', and &)
This is basically a pass-through to the native PHP htmlspecialchars_decode
function with a predefined set of configuration arguments based on current configuration. HTML::escape()
is the inverse of this.
$str
- (string) The string to unescape
Determines if the current profile is an HTML5-compatible one.
Returns true
if the current profile is HTML5-compatible, and false
if it isn't
Determines if the current profile is an HTML4-compatible one.
Returns true
if the current profile is HTML4-compatible, and false
if it isn't
Determines if the current profile is an XML-strict one.
Returns true
if the current profile is XML-strict, and false
if it isn't