2012-10-26 11 views
9

szukam rozwiązania PHP, który pozwoliłby tworzenia HTML w następującym stylu:PHP HTML Creation Biblioteka

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

Co PHP biblioteki mają podobną API? Nie interesuje mnie "Co jest najlepsze ...?" tylko fakt, że API jest porównywalny, jest tym, co chciałbym wiedzieć.

+0

To nie wydaje się zbyt trudne z projekt polegający na własnej implementacji - to byłoby moje podejście. – Vulcan

+0

możliwy duplikat [Jak analizować i przetwarzać HTML z PHP?] (Http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php) – Gordon

+0

nie, nie chcę parsować lub przetwarzać HTML per se - interesuje mnie obiektowa biblioteka * tworzenia * HTML. Nie chodzi tu również o szablony, ale o rozdzielenie obaw. Najlepsze rozwiązanie byłoby oparte wyłącznie na interfejsach i oddzieliło implementację, aby mogło mieć inne wyjście z tego samego kodu. Implementacja oparta na DOM to tylko jedna z możliwych opcji. –

Odpowiedz

1

mam wersję OO nikt w tym czasie, który wygląda tak:

<?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 będzie na to dobrze. jest to php zintegrowane klasy

+0

Celem jest mieć atrybuty bezpośrednio dostępne i nazewnictwo być html "kompatybilny" np /** * Obraz */ klasa Obraz { \t publicznego $ src;. \t $ publicznego alt, \t publicznego $ tytułowy; \t \t funkcja __construct ($ psrc, Palt $, $ ptitle) { \t \t $ to-> src = $ psrc); \t \t $ this-> alt = $ alt; \t \t $ this-> title = $ title; \t} \t } –

+0

Rozumiem. Moim zdaniem najlepsze w tym przypadku będzie ręczne wykonanie kodu html, którego potrzebujesz. – albanx

0

Do tego można użyć DOM object lub napisać własną klasę, co jest łatwiejsze i thnik. Ale to, co próbujesz, to funkcjonalność normalnego systemu szablonów, takiego jak Smarty, Twig lub innego.

jeśli chcesz krótkiego kodu HTML można użyć HAML

+0

Dzięki za wskazanie na HAML - to oczywiście byłby kandydat na część szablonu (jeśli też zostanie dodany do implementacji). Jak podkreślono w moim komentarzu, część szablonu nie jest dla mnie najciekawsza, ale może mieć sens. –