Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Include w Klasie
DowNlOaD_
post 28.05.2010, 18:05:40
Post #1





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 28.02.2010

Ostrzeżenie: (0%)
-----


a wiec tak

zrobilem swoj template engine z cache

mozna wkladac do plikow szablonu zmienne i robic ify

i mam taka funkcje w klasie:

Kod
    public function show($file)
    {
        if (!file_exists($this->cache_dir."cache.".$file))
        {
            $sname = basename($_SERVER['SCRIPT_FILENAME']);
            echo("<b>TemplateEngine Error:</b> File <b>".$this->template_dir.$file."</b> doesn't exist. Error in <b>".$sname."</b>.<br />");
            return 0;
        }
        include($this->cache_dir."cache.".$file);
    }


i jak uzywam np: show("index_body.php"); to pojawia sie tresc pliku ale zmienne z index.php nie chodza

jak to mozna naprawic?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
DowNlOaD_
post 28.05.2010, 18:24:26
Post #2





Grupa: Zarejestrowani
Postów: 48
Pomógł: 0
Dołączył: 28.02.2010

Ostrzeżenie: (0%)
-----


zmienne normalnie chodza

moze dam template.php? bo do konca nie rozumiem cie

Kod
<?php

class template
{
    protected $template_dir;
    protected $cache_dir;
    protected $values = array();

    public function template_dir($template_dir)
    {
        $this->template_dir = $template_dir;
    }

    public function cache_dir($cache_dir)
    {
        $this->cache_dir = $cache_dir;
    }

        public function setvar($key, $value)
    {
            $this->values[$key] = $value;
        }

        public function convert($file)
    {
        if (!file_exists($this->template_dir.$file))
        {
            $sname = basename($_SERVER['SCRIPT_FILENAME']);
            echo("<b>TemplateEngine Error:</b> File <b>".$this->template_dir.$file."</b> doesn't exist. Error in <b>".$sname."</b>.<br />");
            return 0;
        }
        if (!file_Exists($this->cache_dir."cache.".$file))
        {
            $output = file_get_contents($this->template_dir.$file);
            foreach ($this->values as $key => $value)
            {
                $replacement = "{".$key."}";
                $output = str_replace($replacement, $value, $output);
            }
            $replacement = "{if=";
            $replaced = "<?php if(";
            $output = str_replace($replacement, $replaced, $output);
            $replacement = "/}";
            $replaced = ") { ?>";
            $output = str_replace($replacement, $replaced, $output);
            $replacement = "{elseif}";
            $replaced = "<?php } else { ?>";
            $output = str_replace($replacement, $replaced, $output);
            $replacement = "{elseif=";
            $replaced = "<?php } else if(";
            $output = str_replace($replacement, $replaced, $output);
            $replacement = "{/if}";
            $replaced = "<?php } ?>";
            $output = str_replace($replacement, $replaced, $output);
            $fp = fopen($this->cache_dir."cache.".$file, "w+");
            fwrite($fp, $output);
            fclose($fp);
        }
        }
    public function show($file)
    {
        if (!file_exists($this->cache_dir."cache.".$file))
        {
            $sname = basename($_SERVER['SCRIPT_FILENAME']);
            echo("<b>TemplateEngine Error:</b> File <b>".$this->template_dir.$file."</b> doesn't exist. Error in <b>".$sname."</b>.<br />");
            return 0;
        }
        include($this->cache_dir."cache.".$file);
    }
}

?>


i co o tym sadzicie?
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 20:36