Generate any HTML tag with these two functions.

Apr 05
2008

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!

Visit Other Sites!

Find me on other sites...

Archives

All entries, chronologically...

Pages List

General info about this site...