Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP] Include w Klasie
DowNlOaD_
post
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 (1 - 8)
jaslanin
post
Post #2





Grupa: Zarejestrowani
Postów: 511
Pomógł: 143
Dołączył: 13.03.2010
Skąd: Jasło

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


pokaż ten plik index.php i index_body.php


--------------------
Good luck and happy PHP'ing
Go to the top of the page
+Quote Post
DowNlOaD_
post
Post #3





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

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


INDEX.PHP
Kod
<?php

include("template.php");

$logged = 1;

$t = new template;
$t->template_dir("templates/");
$t->cache_dir("cache/");
$t->convert("index_body.php");
$t->show("index_body.php");

?>


INDEX_BODY.PHP
Kod
{if=$logged == 0/}

lol

{elseif=$logged == 1/}

lol2

{/if}


//edit:

dalem $logged = 1; tylko do testow bo to nie jakis duzy moj skrypt tylko tescik template engina mojego

i pojawia sie lol zamiast lol2

Ten post edytował DowNlOaD_ 28.05.2010, 18:20:15
Go to the top of the page
+Quote Post
Spawnm
post
Post #4





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




A gdzie przekazujesz zmienne do funkcji ?
Go to the top of the page
+Quote Post
DowNlOaD_
post
Post #5





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
Spawnm
post
Post #6





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




Czy ty zrozumiałeś co ja napisałem? Nie przekazujesz zmiennych do funkcji/metody w której includujesz szablon...
Go to the top of the page
+Quote Post
marcio
post
Post #7





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


Nie sprawdzalem ale ten str_replace() mnie nie przekonowywuje do konca, do takiego czegos uzywa sie regexp :]

Bo str_replace nie uwzgledni ci wzorca w 100%


--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
Go to the top of the page
+Quote Post
DowNlOaD_
post
Post #8





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

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


no niewiem ;p

mi jakos all chodzi i konwertuje all

a jak chce uzyc show aby potem includowalo plik z cache to zmiennych z index.php jakos nie wykrywa ;/

a jak poprostu uzyje:

Kod
include("cache/cache.index_body.php");


to all chodzi

ale po to zrobilem show() aby skrocic kod a zmiennych z klasy nie moge wyciagnac do include, chyba ze sie da i niewiem jak ;/

//edit

SOLVED smile.gif

moze byc lock ;p

Ten post edytował DowNlOaD_ 28.05.2010, 18:40:45
Go to the top of the page
+Quote Post
jaslanin
post
Post #9





Grupa: Zarejestrowani
Postów: 511
Pomógł: 143
Dołączył: 13.03.2010
Skąd: Jasło

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


w index.php:

  1. $logged = 1;


nie przekazujesz tej zmiennej do klasy/metody. Więc jak się ma tam znaleźć ?


--------------------
Good luck and happy PHP'ing
Go to the top of the page
+Quote Post

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 Aktualny czas: 20.08.2025 - 23:29