Ankündigung

Einklappen
Keine Ankündigung bisher.

Template Klasse

Einklappen

Neue Werbung 2019

Einklappen
X
  • Filter
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge

  • Template Klasse

    Hi

    ich hab mir eine kleine Tremplateklasse geschrieben.

    PHP-Code:
    <?
    error_reporting(E_ALL);
    class template {
        private $content;
        private $teile = array();
        function assign($temname, $str) {
            $number = count($this->teile);
            $this->teile[$number]['temname'] = $temname;
            $this->teile[$number]['str'] = $str;
        }
        function display($file, $typ = 'file') {
            $this->datei($file, $typ);
            $this->chose();
            echo $this->content;
            $this->content = '';
        }
        function gimme($file, $typ = 'file') {
            $this->datei($file, $typ);
            $this->chose();
            $foobar = $this->content;
            $this->content = '';
            return $foobar;
        }
        function datei($file, $typ) {
            if($typ != 'file') {
                $this->content = $file;
            } else {
                $this->content = file_get_contents($file);
            }
        }
        function chose() {
            foreach($this->teile as $teil) {
                if(is_array($teil['str'])) {
                    $this->arr($teil['temname'], $teil['str']);
                } else {
                    $this->str($teil['temname'], $teil['str']);
                }
            }
            $this->teile = array();
        }
        function str($temname, $str) {
            $this->content = str_replace('{'.$temname.'}', $str, $this->content);
        }
        function arr($temname, $arr) {
            $foo = preg_replace('/.*?\{array:'.$temname.'\}(.*?)\{\/array:'.$temname.'\}.*?/Usim','$1',$this->content);
            $foobar = '';
            preg_match_all('/\{'.$temname.':(.+)\}/Usim', $foo, $array);
            foreach($arr as $rr) {
                $bar = '';
                for($i = 0; $i < count($array[0]); $i++) {
                    $arrfile = $array[1][$i];
                    if (empty($bar)) {
                        $bar = str_replace($array[0][$i], $rr[$arrfile], $foo);
                    } else {
                        $bar = str_replace($array[0][$i], $rr[$arrfile], $bar);
                    }
                }
                $foobar .= $bar;
            }
            $this->content = preg_replace('/\{array:'.$temname.'\}.*?\{\/array:'.$temname.'\}/Usim',$foobar,$this->content);
        }
    }
    ?>
    was haltet ihr davon und warum funktioniert sie nicht wenn ich arrays nur mit einem {/array} beenden will?

  • #2
    keiner ne idee?

    Kommentar


    • #3
      wäre durchaus sinnvoll wenn du deinen code mit kommentaren vesieht!

      sprich was wird in der methode gemacht oder wozu dient sie

      Kommentar


      • #4
        Der Grund warum dein regulärer Ausdruck nicht funktioniert ist der, das du nut nach {/array:igenwas} fragst. Du musst die Bedingung erweitern. Hier mal eine funktionsfähige Variante:
        PHP-Code:
        <?php
        $foo 
        preg_replace('/\{array:'.$temname.'\}(.*?)\{\/(array|array:'.$temname.')\}si','$1',$this->content);
        ?>
        http://www.cix88.de/cix_php/demo/cix...ster/index.php zum testen (Mit Dank an CIX88, das ist wirklich klasse zum üben. )

        Ich gebe jens76 recht, da fehlen Kommentare. Außerdem solltest du deine Variablen-/Funktionsnamen nochmal überdenken. Die sind nicht sonderlich sprechend.

        Kommentar

        Lädt...
        X