2012-10-26 14 views
9

मैं एक PHP समाधान निम्न शैली में एचटीएमएल बनाने की अनुमति होगी के लिए खोज रहे हूँ:पीएचपी एचटीएमएल निर्माण लाइब्रेरी

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

क्या पीएचपी पुस्तकालय एक ऐसी ही एपीआई है? मुझे कोई दिलचस्पी नहीं है "सबसे अच्छा क्या है ...?" सिर्फ तथ्य यह है कि एपीआई तुलनीय है जो मैं जानना चाहता हूं।

+0

यह भी मुश्किल की तरह प्रतीत नहीं होता अपना खुद का कार्यान्वयन करने के लिए एक परियोजना - यह मेरा दृष्टिकोण होगा। – Vulcan

+0

संभावित डुप्लिकेट [PHP के साथ एचटीएमएल को कैसे पार्स और प्रोसेस करना है?] (Http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php) – Gordon

+0

नहीं, मैं नहीं चाहता प्रति एचटीएमएल को पार्स या संसाधित करने के लिए - मुझे ऑब्जेक्ट उन्मुख HTML * निर्माण * लाइब्रेरी में रूचि है। यह टेम्पलेट्स के बारे में भी नहीं बल्कि चिंताओं को अलग करने के बारे में भी है। सबसे अच्छा समाधान पूरी तरह से इंटरफेस पर आधारित होगा और कार्यान्वयन को अलग करेगा ताकि उसी कोड से अलग आउटपुट हो सके। एक डीओएम आधारित कार्यान्वयन सिर्फ एक संभावित विकल्प है। –

उत्तर

1

मैं इस समय है कि इस तरह दिखता है पर एक भी नहीं 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"; 
    } 

?> 
0

php http://php.net/manual/en/class.domdocument.php इसके लिए अच्छा होगा। यह php एकीकृत वर्ग

+0

लक्ष्य गुण के लिए सीधे उपलब्ध है और नामकरण एचटीएमएल होने के लिए 'उपयुक्त' जैसे /** * एक छवि */ वर्ग छवि { \t सार्वजनिक $ src;। \t सार्वजनिक $ alt; \t सार्वजनिक $ शीर्षक; \t \t समारोह __construct ($ PSRC, $ palt, $ ptitle) { \t \t $ this-> src = $ PSRC); \t \t $ यह-> alt = $ alt; \t \t $ यह-> शीर्षक = $ शीर्षक; \t} \t } –

+0

मैं समझता हूं। मेरी राय में इन मामलों में सबसे अच्छा मैन्युअल रूप से एचटीएमएल कोड बनाना होगा। – albanx

0

इसके लिए आप DOM object का उपयोग कर सकते हैं या अपनी खुद की कक्षा लिख ​​सकते हैं जो मुझे आसान है। लेकिन आप जो कोशिश करते हैं वह एक सामान्य टेम्पलेट सिस्टम की कार्यक्षमता है जैसे Smarty, Twig या अन्य।

आप इस्तेमाल कर सकते हैं अगर आप कम अपने HTML कोड के लिए चाहते HAML

+0

HAML को इंगित करने के लिए धन्यवाद - यह स्पष्ट रूप से टेम्पलेट भाग के लिए एक कैंडीएट होगा (यदि कार्यान्वयन में भी जोड़ा जाए)। जैसा कि मेरी टिप्पणी में उल्लिखित है, टेम्पलेट भाग मेरे लिए सबसे दिलचस्प नहीं है लेकिन समझ में आता है। –

संबंधित मुद्दे