Zend - The PHP Company




HTML

Add Code


A Nested Tag Generator  

Type: class library
Added by: GabeBauman
Entered: 29/03/2000
Last modified: 08/12/1999
Rating: **** (7 votes)
Views: 8403
Sick of forgetting to close tags when generating tables and HTML documents in a PHP script? This library contains 2 classes:
  • CTagDocument
  • CHTMLDocument (an extension of CTagDocument).
These classes track HTML tags as you open and close them; CHTMLDocument is geared towards generation of a complete HTML document, while CTagDocument can be used for structures like TABLE and LIST. Basic HTML formatting is also done for you. These classes do not buffer their output to memory - they echo() their output to the browser as they go. Call 'CloseAllTags()' to properly close all open tags, or 'CloseTag()' to close the current tag. See the example below...


<?php
// This function is not meant to be called by user
function HTML_MKPARAM_ARR ($string$delimiter ",") {
  
$buf explode($delimiter$string);
  
  for (
$i=0$i<count($buf); $i++) {
    
$buf2 explode ("="$buf[$i]);
    
$result[$buf2[0]] = $buf2[1];
  }
  unset (
$buf);
  unset (
$buf2);
  return 
$result;  
}

// This function is not meant to be called by user
function HTML_MKPARAM_STR ($array) {
  while (list(
$key,$val) = each($array)) {
    if (
$key) {
      
$result .= " " strtolower($key);
    }
    if (
$val != "") {
      
$result .=  "="" . $val . """;
    }
  }
  return 
$result;
}

// This function is not meant to be called by user
function HTML_TAG ($tagclass$params ""$content ""$closenow=false) {
  print 
"<" strtoupper($tagclass);
  if (
is_string($params)) {
    
$params HTML_MKPARAM_ARR($params);
  }
  if (
is_array($params)) {
    print 
HTML_MKPARAM_STR ($params);
  } 
  print 
">" $content;
  if (
$closenow == true) {
    print 
"</" strtoupper($tagclass) . ">";
  }
}

class 
CTagDocument {
  var 
$_current = -1;
  var 
$open_tags = array(); 
  
  function 
OpenTag ($tagclass$params ""$content ""$closenow=false) {
    
HTML_TAG($tagclass$params$content$closenow);
    if (
$closenow == false) {
      
$this->_current++;
      
$this->open_tags[$this->_current] = strtoupper($tagclass);      
    }
  }
  function 
CloseTag () {
    if (
$this->_current > -1) {
      print 
"</" $this->open_tags[$this->_current] . ">n";
      
$this->_current--;
    }
  }
  function 
CloseAllTags() {
    while (
$this->_current > -1) {
      
$this->CloseTag();
    }
  }  
  function 
_ident () {
    return 
"CTagDocument/1.1";
  }
}

class 
CHTMLDocument extends CTagDocument {
  var 
$DocType '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
  function 
CHTMLDocument ($title=""$bodyparams=""){
    print 
$this->DocType "rnrn";
    
$this->OpenTag ("html");
    
$this->OpenTag ("head");
    
$this->OpenTag ("title"""$titletrue);
    
$this->OpenTag ("meta""name=generator,content=CHTMLDocument by G. Bauman");
    
$this->_current--;
    
$this->CloseTag ();
    
$this->OpenTag ("body"$bodyparams);
  }
  function 
_ident () {
    return 
"CHTMLDocument/1.1";
  }
}

?>
?>


Usage Example


// To generate a table: 

$obj = new CTagDocument; 

$obj -> OpenTag ("TABLE","width=100%,bgcolor=silver"); 
$obj -> OpenTag ("TR"); 
$obj -> OpenTag ("TD", array("bgcolor"=>"white"), "Cool Stuff!", true); 
// notice that you can either pass in an  
// associative array or comma-delimited 
// list of tag parameters to OpenTag. 
$obj -> OpenTag ("TD", "", "yeah!", true); 

$obj -> OpenTag ("TD","bgcolor=aqua");

echo "And another column..."; 

$obj -> CloseAllTags (); 


Rate This Script





Search



This Category All Categories