Source of: tutorial/htmlclass/class-html.php (Download Source)
Last Modified: Fri, 16 Feb 2007 13:38:18 UTC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
    class HTML extends HTMLempty
    {
        var $_Inhalt = array();
        function addInhalt($content)
        {
            if($classname = get_class($content))
            {
                $valid_classes = array('htmlempty', 'html');
                if(in_array($classname, $valid_classes))
                {
                    $this->_Inhalt[] = $content;
                }
                else
                {
                    trigger_error('Ungültiges Objekt: "'.$classname.'"', E_USER_ERROR);
                }
            }
            elseif(is_scalar($content))
            {
                $this->_Inhalt[] = (string)$content;
            }
            else
            {
                trigger_error('Parameter muss ein Objekt oder Scalar sein', E_USER_ERROR);            
            }
        }
        function getInhalt()
        {
            return $this->_Inhalt;
        }
        function ausgeben($indent = 0)
        {
            $str_indent = str_repeat(' ', $indent);

            echo($str_indent."<".$this->getName());

            $attribute = $this->getAttribut();
            foreach($attribute as $name => $wert)
            {
                echo(' '.$name.'="'.$wert.'"');
            }
            echo(">\n");

            $content = $this->getInhalt();
            foreach($content as $inhalt)
            {
                if(is_object($inhalt))
                {
                    // der aktuelle Inhalt ist ein Object
                    // also ein HTML-Element. Also geben
                    // wir es aus

                    $inhalt->ausgeben($indent + 4);
                    // Rekursion lässt grüßen ...                
                }
                else
                {
                    // Inhalt ist ein String. Jeden Zeile
                    // geben wir getrennt aus
                    $zeilen = explode("\n", $inhalt);
                    foreach($zeilen as $key => $zeile)
                    {
                        $zeilen[$key] = $str_indent."    ".htmlspecialchars($zeile);
                    }
                    echo(nl2br(implode("\n", $zeilen))."\n");
                }
            }
            echo($str_indent."</".$this->getName().">\n");
        }
    }
?>