Skip to content

Commit

Permalink
HTML - a stupidly simple class for spitting out tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Brennen Bearnes committed Oct 17, 2012
1 parent c4ca8c2 commit f7a2d4b
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions HTML.php
@@ -0,0 +1,54 @@
<?php
namespace SparkLib;

class HTML {
/**
* spit out an HTML tag
*
* takes a tag name, an array of attribute name/values, and an
* optional body.
*
* if an attribute value is a literal true instead of a string,
* just the attribute name will be inserted. this probably isn't
* valid XHTML, but i think it's valid HTML5
*
* @param string tag name
* @param array attribute name/value pairs
* @param string contents of the tag
*/
protected function makeTag ($tagname, array $attributes, $contents = null)
{
$close = ($contents === null) ? true : false;
$html = $this->startTag($tagname, $attributes, $close);

// if we got something to put inside, wrap it up
// otherwise put it in a self-closing tag
// n.b., this is potentially flawed for <script>
if ($close)
return $html;
else
return $html . $contents . "</{$tagname}>";
}

/**
* Make a start tag, optionally self-closing.
*/
protected function startTag ($tagname, array $attributes = array(), $close = false)
{
// open the tag
$html = '<' . $tagname;

// handle attributes
foreach ($attributes as $a => $v) {
if ($v === null) continue; // nothing
elseif ($v === true) $html .= ' ' . $a; // bare attribute, no value
else $html .= ' ' . $a . '="' . htmlspecialchars($v) . '"'; // attribute="value"
}

if ($close)
return $html . ' />';
else
return $html . '>';
}

}

0 comments on commit f7a2d4b

Please sign in to comment.