PHP HTML创建库 [英] PHP HTML Creation Library

查看:113
本文介绍了PHP HTML创建库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ head = new Head( ); 
$ title = new标题(页面标题);
$ 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是可比的是我想知道的。

解决方案

我有一个没有OO版本这一次看起来像这样:

 <?php 
/ **
* HTML抽象
* /

// html
函数html($ html){
返回标记(html,$ html,-1,0);
}

// body
函数体($ body,$ indent = 1){
返回标记(body,$ body,$ indent,$缩进);
}

//头部
函数头($ head,$ indent = 1){
返回标记(head,$ head,$ indent,$缩进);

$ b // image
函数img($ src,$ alt,$ width,$ height,$ indent = -1){
return attrtag( IMG,ATTR( SRC,$ SRC).attr( ALT,$ ALT).attr( 宽度,$宽度).attr( 高度,$高度), ,$缩进,$缩进);
}

//表
函数表($ lt,$ indent = 3){
返回标记(table,$ lt,$ indent,$缩进);
}

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



//带有可能缩进的标签
函数标签($ tag,$ ltagcontent,$ openindent = -1,$ closeindent = - 1){
return attrtag($ tag,,$ ltagcontent,$ openindent,$ closeindent);


函数td($ ltd,$ indent = 5){
返回标记(td,$ ltd,$ indent,$ indent);
}

函数th($ lth,$ indent = 5){
返回标记(th,$ lth,$ indent,$ indent);


函数tr($ ltr,$ indent = 4){
返回标记(tr,$ ltr,$ indent,$ indent);
}

函数a($ href,$ la,$ indent = -1){
return attrtag(a,attr(href,$ href), $ LA,$缩进,$缩进);


函数h($ h,$ lh,$ indent = -1){
if($ indent< 0)
$ indent = $ h + 1;
返回标记(h。$ h,$ lh,$ indent,-1);
}


//具有给定值的属性
//如果未设置值,则为空
函数attr($ attr,$ value ){
if(empty($ value))
return;
else
return。$ attr。='。$ value。';
}

//归因标记,可能缩进
函数attrtag($ tag,$ attr,$ ltagcontent,$ openindent = -1,$ closeindent = -1){
$ html =<。$ tag。$ attr;
if($ openindent> = 0)
$ html =\\\
.indentation($ openindent)。$ html;
if(empty($ ltagcontent)){
$ html。=/>;
if($ closeindent> = 0)
$ html。=\\\
.indentation($ closeindent);
} else {
$ html。=>。$ ltagcontent;
if($ closeindent> = 0){
$ html。=\\\
.indentation($ closeindent);
}
$ html。=< /。$ tag。>;
}
返回$ html;
}

//缩进给定的行
函数缩进($ html,$ indent){
$ result =;
$ lines = explode(\\\
,$ html);
foreach($ lines as $ line){
$ result。= indentation($ indent)。$ line。\\\
;
}
返回$ result;
}


//由给定计数缩进
函数缩进($ count){
return str_repeat(,$ count);
}

//添加换行符
函数行($ line){
return $ line。\\\
;
}

?>


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.

解决方案

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

?>

这篇关于PHP HTML创建库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆