9

我正在寻找一种 PHP 解决方案,它允许以以下样式创建 HTML:

$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();

哪些 PHP 库有类似的 API? 我对“什么是最好的......?”不感兴趣 我想知道 API 具有可比性这一事实。

4

3 回答 3

2

我现在确实有一个无 OO 版本,如下所示:

<?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";
   }

?>
于 2012-10-26T07:46:52.080 回答
0

php http://php.net/manual/en/class.domdocument.php将对此有好处。它是php集成类

于 2012-10-26T07:45:53.490 回答
0

为此,您可以使用DOM 对象或编写我们自己的类,这更容易。但是您尝试的是普通模板系统的功能,例如SmartyTwig 或其他。

如果你想缩短你的 HTML 代码,你可以使用HAML

于 2012-10-26T07:45:58.697 回答