Public feed #890364

Stuff

public text v1 · immutable
Raw Edit
·published 2008-02-03 18:10 UTC
rendered paste body
<?php
/*      template.class.php
 *
 * Description: Seriously simple
 * template class, uses str_replace
 * for finding and replacing matches
 *
 * Author: eax
 * Contact: eax@eax.gopzap.com
 * Compiler: None, interpreted.
 * Interpreter: PHP 5.1.2 CLI
 * Operative System: Any that supports
 * PHP
 */

class Template {
        private $VarList;
        private $FileContent;
        private $FilePath;
        function __construct($F) {
                if (file_exists($F)) { $this->FilePath = $F; return 0; }
                else { return -1; }
        }
        function AddVar($VN, $RL = 'null') { $this->VarList[] = "{$VN}\x01{$RL}"; return 0; }
        function DelVar($VN, $RL = 'null') {
                if (array_key_exists("$VN\x01$RL", $this->VarList)) {
                        foreach ($this->VarList as $del) { if (!strstr("$VN\x01$RL", $del)) { $lcp[] = $del; } }
                        for ($x = 0; $x < count($this->VarList); $x++) { $this->VarList[$x] = null; }
                        for ($x = 0; $x < count($lcp); $x++) { $this->VarList[$x] = $lcp[$x]; }
                        unset($lcp, $del, $x);
                }
                else { return -1; }
        }
        function SetTemplate($file) {
                if (is_file($file)) { $this->FilePath = $file; return 0; }
                else { return -1; }
        }
        function VarExists($VN) {
                if (is_file($this->FilePath)) {
                        $this->FileContent = file_get_contents($this->FilePath);
                        if (preg_match("/\{{$VN}\}/", $this->FileContent)) { return 0; }
                        else { return 1; }
                }
                else { return -1; }
        }
        function Display() {
                if (is_file($this->FilePath)) {
                        $this->FileContent = file_get_contents($this->FilePath);
                        foreach ($this->VarList as $list) { $list = explode("\x01", $list, 2); $VNS[] = "{{$list[0]}}"; $RLS[] = $list[1]; }
                        $content = str_replace($VNS, $RLS, $this->FileContent);
                        if (preg_match_all("/\[(.*)\]/", $content, $match)) {
                                for ($x = 0; $x < count($match[1]); $x++) {
                                        ob_start();
                                        eval($match[1][$x]);
                                        $repl[$x] = ob_get_contents();
                                        ob_end_clean();
                                }
                                for ($x = 0; $x < count($match[1]); $x++) { $match[1][$x] = "[{$match[1][$x]}]"; }
                                $content = str_replace($match[1], $repl, $content);
                        }
                        return $content;
                }
                else { return -1; }
        }
}
?>