TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Template How ? ? (http://www.talkphp.com/general/4900-template-how.html)

hello-world 08-26-2009 09:48 PM

Template How ? ?
 
After a long period of time I understand how to separate php from html.The book php in Action has lots of details about.
But now there is one problem that it is slower than I thought.
Here I have include this file.Can you please tell me why is that so ?

PHP Code:

Class Template {
    public 
$data;
    public 
$file;
    
    public function 
__construct($file,$var,$value){
         
$this->file file_get_contents($file);
         
$this->data[$var] = $value;
         
    }
    
    public function 
set(){
        
//Search for any thing side {} and Save the result in matches(array)
        
preg_match_all("/{.*?}/",$this->file,$matches);
        
        
//loop through the result of matches
        
foreach($matches as $skey => $value){
            
//loop through data
            
foreach($this->data as $val ){
            
            if(
in_array($val,$value)){
                
                foreach(
$this->data as $myKey =>$valo)
                
                
$string =str_replace($val,$myKey,$this->file);
                
                 eval(
"?>".$string);
                
                } else {
                    echo 
"not found";
                    echo eval(
"?>".$this->file);
                    
                     break; 
                }
            }
        }
    }
    
    public function 
parser(){
        
ob_start();
        
$this->set();
        
$string ob_get_contents();
        
ob_get_clean();
        return  
$string;
    }



adamdecaf 08-26-2009 11:01 PM

Isn't eval() slow just like in JS?

Enfernikus 08-26-2009 11:28 PM

Eval is evil, avoid at all cost.

hello-world 08-27-2009 06:36 AM

Quote:

Originally Posted by adamdecaf (Post 28088)
Isn't eval() slow just like in JS?

I don't know anything about JS.
Quote:

Originally Posted by Enfernikus (Post 28091)
Eval is evil, avoid at all cost.

Is there any other way to achieve the same result ??

Enfernikus 08-27-2009 07:49 PM

File cached based on last modified time and output buffering

TheOnly92 08-28-2009 06:09 AM

Why can't you just append those static HTML to a string? It seems to me like you're doing an excessive loop through a series of things...

Enfernikus 08-28-2009 07:47 PM

Business logic and view logic should be strictly separated to facilitate easy ( relatively speaking ) future development and expansion.

hello-world 08-30-2009 11:08 AM

Quote:

Originally Posted by TheOnly92 (Post 28143)
Why can't you just append those static HTML to a string? It seems to me like you're doing an excessive loop through a series of things...

I though if do so.It would be slower using file_get_contents.

Quote:

Originally Posted by Enfernikus (Post 28163)
Business logic and view logic should be strictly separated to facilitate easy ( relatively speaking ) future development and expansion.

I agree with you , but I think I need more practice and your helps.

Thanks anyways.


All times are GMT. The time now is 05:52 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0