Skip to content

Commit 03dcde8

Browse files
committed
Add function html_tag
1 parent c45ef00 commit 03dcde8

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

system/helpers/html_helper.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,47 @@ function nbs($num = 1)
463463
return str_repeat(' ', $num);
464464
}
465465
}
466+
467+
// ------------------------------------------------------------------------
468+
469+
if ( ! function_exists('html_tag'))
470+
{
471+
/**
472+
* Create a XHTML tag
473+
*
474+
* @param string $tag The tag name
475+
* @param array|string $attr The tag attributes
476+
* @param string|bool $content The content to place in the tag, or false for no closing tag
477+
* @return string
478+
*/
479+
function html_tag($tag, $attr = array(), $content = false)
480+
{
481+
// list of void elements (tags that can not have content)
482+
static $void_elements = array(
483+
// html4
484+
"area","base","br","col","hr","img","input","link","meta","param",
485+
// html5
486+
"command","embed","keygen","source","track","wbr",
487+
// html5.1
488+
"menuitem",
489+
);
490+
491+
// construct the HTML
492+
$html = '<'.$tag;
493+
$html .= ( ! empty($attr)) ? ' '.(is_array($attr) ? array_to_attr($attr) : $attr) : '';
494+
495+
// a void element?
496+
if (in_array(strtolower($tag), $void_elements))
497+
{
498+
// these can not have content
499+
$html .= ' />';
500+
}
501+
else
502+
{
503+
// add the content and close the tag
504+
$html .= '>'.$content.'</'.$tag.'>';
505+
}
506+
507+
return $html;
508+
}
509+
}

0 commit comments

Comments
 (0)