File: htmlxlite.class.php

Recommend this page to a friend!
  Classes of Bn Gbor  >  HTML X Lite  >  htmlxlite.class.php  >  Download  
File: htmlxlite.class.php
Role: Class source
Content type: text/plain
Description: htmlxlite HTML code generator
Class: HTML X Lite
Generate HTML pages programatically
Author: By
Last change: Version 4.3:

- Some non-trivial bug fixes
- Shorter code
- Add "noclose" return directive to forces non-closable tag for partial XML support
Date: 7 years ago
Size: 6,305 bytes
 

Contents

Class file image Download
<?php
#############################################################
#
# HTML X LITE - generate HTML (and XML) code programatically
#
# Copyright © Bánó Gábor - 2014
# Email: banogabor72@gmail.com
# Version 4.3
#
# All rights reserved. No part of this publication may be
# reproduced, stored in a retrieval system, or transmitted
# in any form or by any means without the prior written
# permission of the copyright owner and must contain the
# avove copyright notice.
#
#############################################################
define ("DEVELOP_MODE",true);
define ("DOCTYPE","<!DOCTYPE html>");
class
html {
   static
$STACK = array();
   static
$JSMSG = NULL;
   static
$ACTIVE_ELEMENTS = array("button","checkbox","file","hidden","image","password","radio","reset","submit","text","date","number","range","slider");
   static
$NOTCLOSABLE = array("img","input","br","hr","param","meta","link","base","frame","embed","area");
  
# Close all opened tags
  
function finish() {self::closetag(-1);if (self::$JSMSG) self::script(sprintf("txt=alert('%s');",self::$JSMSG));}
  
# Onerow
  
function srow($ONEPARAM = NULL) {
     if (!
$ONEPARAM) return;
    
$PARAMS = explode("|",$ONEPARAM);
     return(
self::tag($PARAMS));
   }
  
#--------------- HTML row generator ----------------
  
function tag() {
     
$RET = $ARGS = $TXT = NULL;
     
$PARAMS = func_get_args();
      if (
is_array($PARAMS[0])) $PARAMS = $PARAMS[0];
      if (!
$PARAMS) return; # No params, end
     
else if (in_array($PARAMS[0],self::$ACTIVE_ELEMENTS)) {$PARAMS[] = 'type='.$PARAMS[0];$PARAMS[0] = 'input';} # Check if tag is an active element
     
$CLBS = in_array($PARAMS[0],self::$NOTCLOSABLE) ? " /" : NULL; # Check if tag is not closable
      # Prepare tag params
     
for ($I = 1 ; $I < count($PARAMS) ; $I++) {
     if (!
$PARAMS[$I]) continue;
    
$ONEATTR = @explode("=",$PARAMS[$I],2);
     if (
$ONEATTR['0'] == "txt") $TXT = sprintf("%s</%s>",$ONEATTR['1'],$PARAMS[0]);
     else if (
$ONEATTR['0'] == "noclose") $CLBS = " /";
         else if (
$ONEATTR['0'] == "ret") $RET = true;
     else
$ARGS .= sprintf(' %s="%s"',$ONEATTR[0],$ONEATTR[1]);
      }
     
# Generate front spaces in develop mode and EOL
     
$FSS = ((DEVELOP_MODE == true) && !$RET) ? str_repeat(" ",count(self::$STACK)) : NULL;
     
$EOL = ((DEVELOP_MODE == true) && !$RET) ? "\n" : NULL;
     
$HTML = sprintf("%s<%s%s%s>%s%s",$FSS,$PARAMS[0],$ARGS,$CLBS,$TXT,$EOL); # Assembly generated HTML row
      # Register current tag into stack if closable and if txt not present
     
if (!$CLBS && !$TXT) self::$STACK[count(self::$STACK)] = $PARAMS[0];
     
# Display or return generated HTML row
     
if (!$RET) echo $HTML; else return $HTML;
   }
  
#------------------ Tag closer --------------------
  
function closetag($NUM = 1,$RET = false) {
     
$HTML = NULL;
     
$EOL = ((DEVELOP_MODE == true) && !$RET) ? "\n" : NULL;
     
$INDEX = count(self::$STACK);
      while (
$NUM-- && $INDEX--) {
       
$FSS = ((DEVELOP_MODE == true) && !$RET) ? str_repeat(" ",$INDEX) : NULL;
    if (
self::$STACK[$INDEX] != "stpt") $HTML .= sprintf("%s</%s>%s",$FSS,self::$STACK[$INDEX],$EOL);
        unset (
self::$STACK[$INDEX]);
      }
      if (!
$RET) echo $HTML; else return $HTML;
   }
  
#------------------- Start block ------------------------
  
function start_block() {self::$STACK[count(self::$STACK)] = "stpt";}
  
#------------------ End block --------------------------
  
function end_block() {
     
$STPT = array_keys(self::$STACK,"stpt");
     
self::closetag(count(self::$STACK)-$STPT[count($STPT)-1]);
   }
  
#------------------ Display header --------------------
  
function display_header() {self::set_header("display");}
  
#------------------ Header preset ---------------------
  
function set_header() {
    
$ARGS = func_get_args();
     static
$HEADER = NULL;
     foreach (
$ARGS as $ONE) {
      
$EXP = explode("=",$ONE,2);
       if ( (
defined("DEVELOP") && (DEVELOP == true) ) && ($EXP['0'] != "display") ) { $HEADER .= " ";$EOL = "\n"; } else $EOL = NULL;
       if (!
$EXP['0']) return;
       else if (
$EXP['0'] == "charset") $HEADER .= self::meta("http-equiv=Content-Type","content=text/html; charset=".$EXP['1'],"ret").$EOL;
       else if (
$EXP['0'] == "description") $HEADER .= self::meta("name=Description","content=".$EXP['1'],"ret").$EOL;
       else if (
$EXP['0'] == "keywords") $HEADER .= self::meta("name=keywords","content=".$EXP['1'],"ret").$EOL;
       else if (
$EXP['0'] == "refresh") $HEADER .= self::meta("http-equiv=refresh","content=".$EXP['1'],"ret").$EOL;
       else if (
$EXP['0'] == "language") $HEADER .= self::meta("http-equiv=language","content=".$EXP['1'],"ret").$EOL;
       else if (
$EXP['0'] == "nocache") $HEADER .= self::meta("http-equiv=cache-control","content=no-cache","ret").$EOL;
       else if (
$EXP['0'] == "title") $HEADER .= self::title("txt=". $EXP['1'],"ret").$EOL;
       else if (
$EXP['0'] == "css") $HEADER .= self::link("rel=stylesheet","type=text/css","href=".$EXP['1'],"ret").$EOL;
       else if (
$EXP['0'] == "script") $HEADER .= self::script("type=text/javascript","txt=".$EXP['1'],"ret").$EOL;
       else if (
$EXP['0'] == "icon") $HEADER .= self::link("rel=shortcut icon","href=".$EXP['1'],"ret").$EOL;
       else if (
$EXP['0'] == "link") $HEADER .= self::srow("link|".$EXP['1']."|ret").$EOL; # (ie.: link=name=content|href=2|param2=3)
      
else if ($EXP['0'] == "meta") $HEADER .= self::srow("meta|".$EXP['1']."|ret").$EOL; # (ie.: meta=name=content|param=2|param2=3)
      
else if ($EXP['0'] == "js") $HEADER .= self::script("type=text/javascript","src=".$EXP['1'],"txt=","ret").$EOL;
       else if (
$EXP['0'] == "display") {print DOCTYPE."\n"; self::html();self::head(); print $HEADER;self::closetag();}
       else {
$HEADER .= self::tag(array_merge(func_get_args(),array("ret"))).$EOL;break;}
     }
   }
  
#---------------- Addional elements -------------------
  
function write($TXT) {print (str_repeat(" ",count(self::$STACK)).$TXT."\n");}
   function
remark($TXT) { if (DEVELOP_MODE == true) self::write("<!--".$TXT."-->"); }
   function
message($TXT) { self::$JSMSG .= $TXT; }
  
#--------------- Create any other tag -----------------
  
static function __callStatic($name, $arguments) {return(self::tag(array_merge(array($name),$arguments)));}
}
?>

For more information send a message to info at phpclasses dot org.