2012-06-23 89 views
4

我是一个PHP newb。很抱歉,如果这是一个常见问题...用于生成HTML标记的PHP库

比方说,我有这样的HTML表:

<table width="100%" cellpadding="12" cellspacing="0" border="0"> 
    <tr bgcolor="black"> 
    <td align="left"> 
     <img src="logo.gif" /> 
    </td> 
    <td> 
     <h1>Hello</h1> 
    </td> 
    <td align="right"> 
     <img src="logo.gif" /> 
    </td> 
    </tr> 
</table> 

而是逃到HTML(?> ... <?php)或使用回声手工串的建设,我想使用PHP函数来生成代码。我cooked up a library使上面的例子可以用此产生:

echo table(
    array('width' => '100%', 'cellpadding' => '12', 'cellspacing' => '0', 'border' => '0'), 
    tr(
     array('bgcolor' => 'black'), 
     td(
     array('align' => 'left'), 
     img(array('src' => 'logo.gif'))), 
     td(array(), h1(array(), 'Hello')), 
     td(array('align' => 'right'), img(array('src' => 'logo.gif'))))); 

我的问题是,有没有已经做这个流行的或常用库?

+1

如果没坏。不要修复它。你写它,它的作品。那很棒!为什么要找一个图书馆? –

+1

有趣的图书馆。我喜欢嵌套函数调用匹配预期的输出结构的想法。我很想知道这将如何执行大量嵌套的HTML。 – bejonbee

+1

纯HTML占99%的HTML有什么问题?你的PHP版本中有大约80多个字符,造成了更多的解析开销,而且真的,但真的很丑。 (并且难以阅读) –

回答

2

我没有找到一个图书馆,所以我继续更新我自己的HTML tab library

+1

您可能想要考虑自动对您的属性值和标记内容进行HTML编码。你可以有一个可选参数来跳过编码。而当您在标签中包含其他标签时,或者在使用

2

codeigniter做这种事情。 我建议你看看。

2

PHP HTML Creation Library非OO-答:

使用example.php:

<?php 
require ("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 html 
    echo html($head.$body); 
?> 

html.php:

<?php 
/** 
* HTML Abstraction 
*/ 

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

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

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

    // horizontal ruler 
    function hr($indent=-1) { 
    return tag("hr","",$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); 
    } 

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

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

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

    // table row 
    function tr($ltr,$indent=4) { 
    return tag("tr",$ltr,$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); 
    } 

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

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

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

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

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

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

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

?>