Saturday, April 5, 2008

Generate any HTML tag with these two functions.

I was working on a form generation API on Typhoon PHP Framework today and came up with two wonderful functions for generating any HTML tag using PHP.

Here are the functions, enjoy!:


public static function tag($element,$attributes=array())
{
$buffer = '<' . $element;
foreach($attributes as $key => $val) {
$buffer .= ' ' . $key . '="' . $val . '"';
}
$buffer .= '/>';

return $buffer;
}

public static function blocktag($element,$attributes=array())
{
$content = $attributes['content'];
unset($attributes['content']);

$buffer = '<' . $element;
foreach($attributes as $key => $val) {
$buffer .= ' ' . $key . '="' . $val . '"';
}
$buffer .= '>';

$buffer .= $content;
$buffer .= '</' . $element . '>';

return $buffer;
}


Currently I use these in conjunction with Smarty helpers to create consistent markup across a project I'm working on. Any thoughts are appreciated!

No comments: