Really boring HTML & XML tag class function for PHP

That’s right. It’s simple boring and reusable. I used it for the complex HTML need to make the WP Super Edit WordPress administration options. It’s actually a function, but I use it inside a PHP class when I know I’m going to be either doing nasty HTML echo’s or returning complex HTML formatted variables. It’s not a template system or anything like that, I just needed something without the massive dependencies and overhead of a full fledged HTML / Dom / Pear class.

I’ve put it into a simple class for this example, and you can download the text file in case my site mangles the code: Real Boring HTML & XML Class PHP Text File

Oh yea, and since a tag is a tag, then this can also do simple XML.

[php]class boring_tag_creator {

/**
* Display or return html or xml tag with attributes.
* @param array $tag_options options and content to display
* @return mixed
*/
function the_tag( $tag_options = array() ) {

$attributes = ”;
$composite = ”;

foreach ( $tag_options as $name => $option ) {
if ( $name == ‘tag’ ) continue;
if ( $name == ‘content’ ) continue;
if ( $name == ‘return’ ) continue;
if ( $name == ‘tag_type’ ) continue;
$tag_attributes .= sprintf( ‘ %s="%s"’, $name, $option );
}

switch ( $tag_options[‘tag_type’] ) {
case ‘single’:
$format = ‘%3$s <%1$s%2$s />’ ;
break;
case ‘single-after’:
$format = ‘<%1$s%2$s /> %3$s’ ;
break;
case ‘open’:
$format = ‘<%1$s%2$s>%3$s’;
break;
case ‘close’:
$format = ‘%3$s<!–%1$s–>’;
break;
default:
$format = ‘<%1$s%2$s>%3$s<!–%1$s–>’;
break;
}

$composite = sprintf( $format, $tag_options[‘tag’], $tag_attributes, $tag_options[‘content’] );

if ( $tag_options[‘return’] == true ) return $composite ;

echo $composite;
}

/**
* Boring Header and paragraph either echoed or returned…
*/
function boring_html( $header, $content, $return = false ) {

$the_h2 = array(
‘tag’ => ‘h2’,
‘content’ => $header
);

$the_content = array(
‘tag’ => ‘p’,
‘id’ => ‘boring_content_id’,
‘style’ => ‘border: 1px solid red’,
‘class’ => ‘boring_content_class’,
‘content’ => $content
);

if ( $return ) {
$the_h2[ ‘return’ ] = true;
$the_content[ ‘return’ ] = true;
return $this->the_tag( $the_h2 ) . $this->the_tag( $the_content );
} else {
$this->the_tag( $the_h2 );
$this->the_tag( $the_content );
}

}

}

$boring = new boring_tag_creator;

$boring->boring_html( ‘The Title’, ‘Some words about this for the content.’ );

$boring_html_variable = $boring->boring_html( ‘Another Title’, ‘More words about this other thing for the content.’, true );

echo $boring_html_variable;[/php]

The class function the_tag takes an array. In that array there are only three special keys.

Whatever else you put as part of the array becomes an attribute for the tag. Simple, boring, and might be useful. Like I said, boring, but this has been useful for me to construct some complex HTML and XML structures.

2 responses to “Really boring HTML & XML tag class function for PHP”

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.