9

I am Looking for a PHP solution that would allow creating HTML in the following style:

$head=new Head();
$title=new Title("The title of the page");
$head->setTitle($title);

$body=new Body();
$h1=new H(1,"Header 1");
$body->add($h1);

$html=new HTML();
$html->setHead($head);
$html->setBody($body);

echo $html->asHTMLString();

What PHP Libraries have a similar API? I am not interested in "What is the best ...?" just the fact that the API is comparable is what I'd like to know.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • It doesn't seem like too difficult of a project to make your own implementation of--that'd be my approach. – FThompson Oct 26 '12 at 07:44
  • possible duplicate of [How to parse and process HTML with PHP?](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php) – Gordon Oct 26 '12 at 08:29
  • no I do not want to parse or process HTML per se - i am interested in an object oriented HTML *creation* library. It is also not so much about templates but about separation of concerns. The best solution would be purely based on interfaces and separate the implementation so that on could have different output from the same code. A DOM based implementation is just one possible option. – Wolfgang Fahl Oct 26 '12 at 08:53
  • 1
    Every good question is closed and marked non constructive ? :( – Ahmed Apr 01 '15 at 23:43

3 Answers3

2

I do have a none OO version at this time that looks like this:

<?php
/**
 * HTML Abstraction
 */

   // html
   function html($html) {
     return tag("html",$html,-1,0);
   } 

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

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

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

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

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



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

   function td($ltd,$indent=5) {
     return tag("td",$ltd,$indent,$indent);
   }

   function th($lth,$indent=5) {
     return tag("th",$lth,$indent,$indent);
   }

   function tr($ltr,$indent=4) {
     return tag("tr",$ltr,$indent,$indent);
   }

   function a($href,$la,$indent=-1) {
     return attrtag("a",attr("href",$href),$la,$indent,$indent);
   }

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


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

   // attributed tag, possibly indented
   function attrtag($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;
   }

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


   // indentation by the given count
   function indentation($count) {
     return str_repeat("  ",$count);
   }

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

?>
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
0

php http://php.net/manual/en/class.domdocument.php will be good for this. it is php integrated classes

albanx
  • 6,193
  • 9
  • 67
  • 97
  • The goal is to have the attributes directly available and the naming to be html "compatible". e.g. /** * an Image */ class Image { public $src; public $alt; public $title; function __construct($psrc,$palt,$ptitle) { $this->src=$psrc); $this->alt=$alt; $this->title=$title; } } – Wolfgang Fahl Oct 26 '12 at 07:51
  • I understand. In my opinion the best in this cases will be to make manually the html code you need. – albanx Oct 26 '12 at 08:22
0

For this you could use the DOM object or write our own class what is easier i thnik. But what you try is the functionality of a normal template system like Smarty, Twig or other.

if you want to short your HTML code you could use HAML

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • Thanks for pointing to HAML - that would obviously be a candiate for the template part (if too be added to the implementation). As outlined in my comment the template part is not the most interesting for me but could make sense. – Wolfgang Fahl Oct 26 '12 at 10:22