Templateing cache issues

Discussion in 'Web Design and Development' started by Cabbit, Apr 30, 2009.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    ^_^ hello i am building a CMS as a wee project and have ran up against a problem. I am using Jpmaster77's login system and have built a templateing engine around it and with the aid of a few tutorials.
    Problem is i don't know how to make the engine not cache some areas, i have managed to spit the page up for different cache expires but not to not cache areas completely which kinda messes up the login as its trying to send template data that is well empty.
    Would appreciate a once over on this maybe someone can help with a switch in the class to not cache the page.

    Here is a link to the project link
    Full source code link


    And here is the class
    PHP:
    <?php
    /************************************************************************************
                                     Kittenbunny CMS
                                     Template Engine 
                                    Openkitten licence free for all
    ************************************************************************************/

    // Prevent direct access to file //
    if(eregi(basename(__FILE__),$_SERVER['REQUEST_URI']))
    die(
    'Direct access prohibited');

    class 
    templateParser 
    {
        
    // Set the output variable
        
    var $output;

        
    // function to set the template and cache file
        
    function templateParser($tags=array(),$cacheFile='default_cache.txt',$expireTime=3600,
        
    $templateFile='default_template.htm')
        {
            if(!
    $this->output=$this->readCache($cacheFile,$expireTime))
            {
                (
    file_exists($templateFile))?$this->output=file_get_contents($templateFile)
                :die
                (
    'Error:Template file '.$templateFile.' not found');
                   
    $this->parseTemplate($tags,$cacheFile);
            }
        }
        
        
    // Function to sent the content to the template
        
    function parseTemplate($tags,$cacheFile)
        {
            if(
    count($tags)>0)
            {
                foreach(
    $tags as $tag=>$data)
                {
                    
    $data=(file_exists($data))?$this->parseFile($data):$data;
                    
    $this->output=str_replace('{'.$tag.'}',$data,$this->output);
                   }
                    
    $this->writeCache($cacheFile,$this->output);
                  }
                  else 
                {
                   die(
    'Error: No tags or files were provided for replacement');
              }
        }
        function 
    parseFile($file)
        {
            
    ob_start();
            include(
    $file);
            
    $content=ob_get_contents();
            
    ob_end_clean();
            return 
    $content;
        }
        function 
    writeCache($cacheFile,$content)
        {
            
    $fp=fopen($cacheFile,'w');
            
    fwrite($fp,$content);
            
    fclose($fp);
        }
        function 
    readCache($cacheFile,$expireTime)
        {
            if (
    file_exists($cacheFile) && filemtime($cacheFile)>(time()-$expireTime))
            {
                 return 
    file_get_contents($cacheFile);
               }
            return 
    false;
        }
        function 
    display()
        {
            return 
    $this->output;
        }
    }
    ?>
    and here is index.php
    PHP:
    <?php
    /************************************************************************************
                                     Kittenbunny CMS
                                     Main page 
    ************************************************************************************/

    //************************* Include Classes ************************************//

    // include the session
    include("./include/settings/session.php");

    // include template parser class //
    require_once('./include/style/template.php');

    //************************* Site Settings ************************************//

    // Connect to site details table
    $sql"SELECT `master`, `theme`, `description`, `keywords`, `abstract`, `title`, `sub_title` FROM `tbl_site-details` LIMIT 1";
    $result mysql_query($sql) or die("Sorry there seems to be a problem with the database."); 
    $settings mysql_fetch_array($result);

    // Selected Theme
    $site['theme'] = $settings['theme'];

    // Meta Data
    $meta['description'] = $settings['description'];
    $meta['keywords'] = $settings['keywords'];
    $meta['author'] = $settings['master'];
    $meta['abstract'] = $settings['abstract'];


    // Header Title
    $site['title'] = $settings['title'];

    //************************* Page location ************************************//

    include('include/settings/active_modules.php');


    // Server document root //
    $self '\../';

    // define class parameters for header //
    $header_tags=array
                (
                
    // Site information //
                    // Site webmaster //
                    
    'webmaster'=>$meta['author'],
                    
    // Site description //
                    
    'description'=>$meta['description'],
                    
    // Site webmaster //
                    
    'keywords'=>$meta['keywords'],
                    
    // Site abstract //
                    
    'abstract'=>$meta['abstract'],
                    
    // Page location //
                    
    'self'=>$self,
                
    // Page content //
                    // Page title //
                    
    'title'=>$title,
                    
    // Page Header //
                    
    'header'=>'./include/style/header.php'
                
    );
                    
    // define class parameters for navagation //
    $navagation_tags=array
                (
                    
    // Primary Navagation bar //
                    
    'main_nav'=>'./include/style/main_nav.php',
                    
                    
    // Secondary tier Navagation bar //
                    
    'sub_nav'=>'./include/style/sub_nav.php'
                
    );

    // define class parameters for navagation //
    $sidebar_tags=array
                (
                    
    // Left block //
                    
    'leftcontent'=>'./include/style/leftcontent.php'
                
    );
                
    // define class parameters for navagation //
    $content_tags=array
                (
                    
    // Page contents
                    
    'maincontent'=>$module
                
    );
                
    // define class parameters for navagation //
    $footer_tags=array
                (
                    
    // Page Footer //
                    
    'footer'=>'./include/style/footer.php'
                
    );
                    

    // instantiate a new template parser object specifying a cache file valid for 2 hours //
    $header=&new templateParser($header_tags,'cache/cache_header_'.$page.'.txt',2*0,'./include/style/'.$site['theme'].'/header.tpl.htm');

    $navagation=&new templateParser($navagation_tags,'cache/cache_navagation_'.$page.'.txt',2*0,'./include/style/'.$site['theme'].'/navagation.tpl.htm');


    // Session logged in //
        
    $side_bar=&new templateParser($sidebar_tags,'cache/cache_sidebar_'.$page.'.txt',2*0,'./include/style/'.$site['theme'].'/sidebar.tpl.htm');

        
    $content=&new templateParser($content_tags,'cache/cache_content_'.$page.''.$subpage.'.txt',$cache_expire,'./include/style/'.$site['theme'].'/content.tpl.htm');

    $footer=&new templateParser($footer_tags,'cache/cache_footer.txt',2*0,'./include/style/'.$site['theme'].'/footer.tpl.htm');


    //*************************** display finished page ******************************//

    // Page header //
    echo $header->display();

    // Navagation Bar //
    echo $navagation->display();

    // Content area //
    echo $content->display();

    // Side bar //
    echo $side_bar->display();

    // footer //
    echo $footer->display();
    ?>


    Oh my thinking is to do something like
    if (isset($nocache))
    {
    $content=&new templateParser($content_tags,'./include/style/'.$site['theme'].'/content.tpl.htm');

    }
    else
    {
    $content=&new templateParser($content_tags,'cache/cache_content_'.$page.''.$subpage.'.txt',$cache_expire,'./include/style/'.$site['theme'].'/content.tpl.htm');
    }
     
  2. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #2
    ok well i went with using two classes, one for cached version and one for un-cached. my only problem is getting them playing nice together i think.

    unfortunately looks like there not playing nice together.


    PHP:
    <?php
    /************************************************************************************
                                     Kittenbunny CMS
                                     Template Engine 
                                    Open licence free for all
    ************************************************************************************/

    // Prevent direct access to file //
    if(eregi(basename(__FILE__),$_SERVER['REQUEST_URI']))
    die(
    'Direct access prohibited');


    // cached version
    class templateParser 
    {
        
    // Set the output variable
        
    var $output;

        
    // function to set the template and cache file
        
    function templateParser($tags=array(),$cacheFile='default_cache.txt',$expireTime=3600,
        
    $templateFile='default_template.htm')
        {
            if(!
    $this->output=$this->readCache($cacheFile,$expireTime))
            {
                (
    file_exists($templateFile))?$this->output=file_get_contents($templateFile)
                :die
                (
    'Error:Template file '.$templateFile.' not found');
                   
    $this->parseTemplate($tags,$cacheFile);
            }
        }
        
        
    // Function to sent the content to the template
        
    function parseTemplate($tags,$cacheFile)
        {
            if(
    count($tags)>0)
            {
                foreach(
    $tags as $tag=>$data)
                {
                    
    $data=(file_exists($data))?$this->parseFile($data):$data;
                    
                    
    // this is the cached version
                   
    $this->output=str_replace('{'.$tag.'}',$data,$this->output);
                         
                   }
                       
    // cached 
                    
    $this->writeCache($cacheFile,$this->output);

                  }
                  else 
                {
                   die(
    'Error: No tags or files were provided for replacement');
              }
        }
        function 
    parseFile($file)
        {
            
    ob_start();
            include(
    $file);
            
    $content=ob_get_contents();
            
    ob_end_clean();
            return 
    $content;
        }
        function 
    writeCache($cacheFile,$content)
        {
            
    $fp=fopen($cacheFile,'w');
            
    fwrite($fp,$content);
            
    fclose($fp);
        }
        function 
    readCache($cacheFile,$expireTime)
            {
            if (
    file_exists($cacheFile) && filemtime($cacheFile)>(time()-$expireTime))
            {
                 return 
    file_get_contents($cacheFile);
               }
            return 
    false;
        }
        function 
    display()
            {
            return 
    $this->output;
        }
    }

    // uncached version
    class templateParser_nocache {

            var 
    $output;

            function 
    templateParser($templateFile='default_template.htm')
            {
                        (
    file_exists($templateFile))?$this->output=file_get_contents($templateFile)
                            :die
                    (
    'Error:Template file '.$templateFile.' not found');

            }

            function 
    parseTemplate($tags=array()){

                  if(
    count($tags)>0)
                  {

                       foreach(
    $tags as $tag=>$data)
                       {
                                                
    $data=(file_exists($data))?$this->parseFile($data):$data;
                                                
    $this->output=str_replace('{'.$tag.'}',$data,$this->output);
                       }
                  }
                  else 
                  {
                       die(
    'Error: No tags were provided for replacement');
                  }
            }

            function 
    parseFile($file)
            {
                  
    ob_start();
                  include(
    $file);
                  
    $content=ob_get_contents();
                  
    ob_end_clean();
                  return 
    $content;
            }

            function 
    display(){
                  return 
    $this->output;
            }

        }
    ?>

    PHP:
    <?php
    /************************************************************************************
                                     Kittenbunny CMS
                                     Main page 
    ************************************************************************************/

    //************************* Include Classes ************************************//

    // include the session
    include("./include/settings/session.php");

    // include template parser class //
    require_once('./include/style/template.php');

    //************************* Site Settings ************************************//

    // Connect to site details table
    $sql"SELECT `master`, `theme`, `description`, `keywords`, `abstract`, `title`, `sub_title` FROM `tbl_site-details` LIMIT 1";
    $result mysql_query($sql) or die("Sorry there seems to be a problem with the database."); 
    $settings mysql_fetch_array($result);

    // Selected Theme
    $site['theme'] = $settings['theme'];

    // Meta Data
    $meta['description'] = $settings['description'];
    $meta['keywords'] = $settings['keywords'];
    $meta['author'] = $settings['master'];
    $meta['abstract'] = $settings['abstract'];


    // Header Title
    $site['title'] = $settings['title'];

    //************************* Page location ************************************//

    include('include/settings/active_modules.php');


    // Server document root //
    $self '\../';

    // define class parameters for header //
    $header_tags=array
                (
                
    // Site information //
                    // Site webmaster //
                    
    'webmaster'=>$meta['author'],
                    
    // Site description //
                    
    'description'=>$meta['description'],
                    
    // Site webmaster //
                    
    'keywords'=>$meta['keywords'],
                    
    // Site abstract //
                    
    'abstract'=>$meta['abstract'],
                    
    // Page location //
                    
    'self'=>$self,
                
    // Page content //
                    // Page title //
                    
    'title'=>$title,
                    
    // Page Header //
                    
    'header'=>'./include/style/header.php'
                
    );
                    
    // define class parameters for navagation //
    $navagation_tags=array
                (
                    
    // Primary Navagation bar //
                    
    'main_nav'=>'./include/style/main_nav.php',
                    
                    
    // Secondary tier Navagation bar //
                    
    'sub_nav'=>'./include/style/sub_nav.php'
                
    );

    // define class parameters for navagation //
    $sidebar_tags=array
                (
                    
    // Left block //
                    
    'leftcontent'=>'./include/style/leftcontent.php'
                
    );
                
    // define class parameters for navagation //
    $content_tags=array
                (
                    
    // Page contents
                    
    'maincontent'=>$module
                
    );
                
    // define class parameters for navagation //
    $footer_tags=array
                (
                    
    // Page Footer //
                    
    'footer'=>'./include/style/footer.php'
                
    );
                    

    // instantiate a new template parser object specifying a cache file valid for 2 hours //
    $header=&new templateParser($header_tags,'cache/cache_header_'.$page.'.txt',2*0,'./include/style/'.$site['theme'].'/header.tpl.htm');
    $navagation=&new templateParser($navagation_tags,'cache/cache_navagation_'.$page.'.txt',2*0,'./include/style/'.$site['theme'].'/navagation.tpl.htm');

        
    $side_bar=&new templateParser($sidebar_tags,'cache/cache_sidebar_'.$page.'.txt',2*0,'./include/style/'.$site['theme'].'/sidebar.tpl.htm');

        if (
    $page == "login")
        {
        
            
    $content=&new templateParser_nocache('./include/style/'.$site['theme'].'/content.tpl.htm');
            
    $content->parseTemplate($content_tags);
        }
        else
        {
            
    $content=&new templateParser($content_tags,'cache/cache_content_'.$page.''.$subpage.'.txt',$cache_expire,'./include/style/'.$site['theme'].'/content.tpl.htm');    
        }


    $footer=&new templateParser($footer_tags,'cache/cache_footer.txt',2*0,'./include/style/'.$site['theme'].'/footer.tpl.htm');


    //*************************** display finished page ******************************//

    // Page header //
    echo $header->display();

    // Navagation Bar //
    echo $navagation->display();

    // Content area //
    echo $content->display();

    // Side bar //
    echo $side_bar->display();

    // footer //
    echo $footer->display();
    ?>


     

Share This Page