Skip to content Skip to sidebar Skip to footer

Php Library For Html Tag Generation

I'm a PHP newb. Sorry if this is an FAQ... Let's say I have this HTML table:

Solution 1:

You can use Codeigniter html tables and form helper but that's about it. The rest is up to you.

Solution 2:

See non OO-Answer of PHP HTML Creation Library:

example.php:

<?phprequire ("html.php");
  // crete a header$head=head(title("This is an example"));
  // and a body$body=body(h(1,"This is a header 1").pre("With some preformatted text").hr());
  // wrap it in htmlecho html($head.$body);
?>

html.php:

<?php/**
 * HTML Abstraction
 */// anchor   functiona($href,$la,$indent=-1) {
     return attrtag("a",attr("href",$href),$la,$indent,$indent);
   }

   // breakfunctionbr($indent=-1) {
     return tag("br","",$indent,$indent);
   }

   // headerfunctionh($h,$lh,$indent=-1) {
     if ($indent<0) 
       $indent=$h+1;
     return tag("h".$h,$lh,$indent,-1);
   }

   // horizontal rulerfunctionhr($indent=-1) {
     return tag("hr","",$indent,$indent);
   }

   // imagefunctionimg($src,$alt,$width,$height,$indent=-1) {
     return attrtag("img",attr("src",$src).attr("alt",$alt).attr("width",$width).attr("height",$height),"",$indent,$indent);
   }

   // pre formatted contentfunctionpre($content,$indent=-1) {
     return tag("pre",$content,$indent);
   }

   // table functiontd($ltd,$indent=5) {
     return tag("td",$ltd,$indent,$indent);
   }

   // table headerfunctionth($lth,$indent=5) {
     return tag("th",$lth,$indent,$indent);
   }

   // table rowfunctiontr($ltr,$indent=4) {
     return tag("tr",$ltr,$indent,$indent);
   }


   // tablefunctiontable($lt,$indent=3) {
     return tag("table",$lt,$indent,$indent);
   }

   // titlefunctiontitle($title,$indent=2) {
     return tag("title",$title,$indent,-1);
   }

   // headfunctionhead($head,$indent=1) {
     return tag("head",$head,$indent,$indent);
   }

   // bodyfunctionbody($body,$indent=1) {
     return tag("body",$body,$indent,$indent);
   }

   // htmlfunctionhtml($html) {
     return tag("html",$html,-1,0);
   } 
   // indentation by the given countfunctionindentation($count) {
     return str_repeat("  ",$count);
   }

   // adds a newline    functionline($line) {
     return$line."\n";
   }

   // an attribute with a given value// or empty if value is not setfunctionattr($attr,$value) {
     if (empty($value))
       return"";
     elsereturn" ".$attr."='".$value."'";
   }

   // attributed tag, possibly indentedfunctionattrtag($tag,$attr,$ltagcontent,$openindent=-1,$closeindent=-1) {
    $html="<".$tag.$attr;
    if ($openindent>=0)
      $html="\n".indentation($openindent).$html;
    if (empty($ltagcontent)) {
      $html.="/>";
        if ($closeindent>=0)
          $html.="\n".indentation($closeindent);
    } else {
        $html.=">".$ltagcontent;
        if ($closeindent>=0) {
          $html.="\n".indentation($closeindent);
        }
        $html.="</".$tag.">";
    }
    return$html;
   }

   // tag with possible indentationfunctiontag($tag,$ltagcontent,$openindent=-1,$closeindent=-1) {
      return attrtag($tag,"",$ltagcontent,$openindent,$closeindent);
   }

    // indent the given linesfunctionindent($html,$indent) {
     $result="";
     $lines=explode("\n",$html);
     foreach($linesas$line) {
       $result.=indentation($indent).$line."\n"; 
     }
     return$result;
   }

?>

Solution 3:

codeigniter does this kind of stuff. i’d suggest you look into that.

Solution 4:

While not an exact match, the Yii framework uses the CHtml class to assist in generating html.

Solution 5:

I didn't find a library so I've continued to update my own HTML tab library.

Post a Comment for "Php Library For Html Tag Generation"