Advertisement
rainbowdash28

view.class.php

Feb 9th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.71 KB | None | 0 0
  1. <?php
  2. class view {
  3.     var $vars; /// Holds all the template variables
  4.  
  5.     /**
  6.      * Constructor
  7.      *
  8.      * @param $file string the file name you want to load
  9.      */
  10.     function view($file = null) {
  11.         $this->file = $file;
  12.     }
  13.  
  14.     /**
  15.      * Set a template variable.
  16.      */
  17.     function set($name, $value) {
  18.         $this->vars[$name] = is_object($value) ? $value->fetch() : $value;
  19.     }
  20.  
  21.     /**
  22.      * Open, parse, and return the template file.
  23.      *
  24.      * @param $file string the template file name
  25.      */
  26.     function fetch($file = null) {
  27.         if(!$file) $file = $this->file;
  28.  
  29.         if(isset($this->vars)) extract($this->vars);          // Extract the vars to local namespace
  30.         ob_start();                    // Start output buffering
  31.         include('./templates/'.$file.'.tpl');                // Include the file
  32.         $contents = ob_get_contents(); // Get the contents of the buffer
  33.         ob_end_clean();                // End buffering and discard
  34.         echo $contents;              // Return the contents
  35.     }
  36. }
  37.  
  38. /**
  39.  * An extension to Template that provides automatic caching of
  40.  * template contents.
  41.  */
  42. class CachedTemplate extends view {
  43.     var $cache_id;
  44.     var $expire;
  45.     var $cached;
  46.  
  47.     /**
  48.      * Constructor.
  49.      *
  50.      * @param $cache_id string unique cache identifier
  51.      * @param $expire int number of seconds the cache will live
  52.      */
  53.     function CachedTemplate($cache_id = null, $expire = 900) {
  54.         $this->Template();
  55.         $this->cache_id = $cache_id ? 'cache/' . md5($cache_id) : $cache_id;
  56.         $this->expire   = $expire;
  57.     }
  58.  
  59.     /**
  60.      * Test to see whether the currently loaded cache_id has a valid
  61.      * corrosponding cache file.
  62.      */
  63.     function is_cached() {
  64.         if($this->cached) return true;
  65.  
  66.         // Passed a cache_id?
  67.         if(!$this->cache_id) return false;
  68.  
  69.         // Cache file exists?
  70.         if(!file_exists($this->cache_id)) return false;
  71.  
  72.         // Can get the time of the file?
  73.         if(!($mtime = filemtime($this->cache_id))) return false;
  74.  
  75.         // Cache expired?
  76.         if(($mtime + $this->expire) < time()) {
  77.             @unlink($this->cache_id);
  78.             return false;
  79.         }
  80.         else {
  81.             /**
  82.              * Cache the results of this is_cached() call.  Why?  So
  83.              * we don't have to double the overhead for each template.
  84.              * If we didn't cache, it would be hitting the file system
  85.              * twice as much (file_exists() & filemtime() [twice each]).
  86.              */
  87.             $this->cached = true;
  88.             return true;
  89.         }
  90.     }
  91.  
  92.     /**
  93.      * This function returns a cached copy of a template (if it exists),
  94.      * otherwise, it parses it as normal and caches the content.
  95.      *
  96.      * @param $file string the template file
  97.      */
  98.     function fetch_cache($file) {
  99.         if($this->is_cached()) {
  100.             $fp = @fopen($this->cache_id, 'r');
  101.             $contents = fread($fp, filesize($this->cache_id));
  102.             fclose($fp);
  103.             return $contents;
  104.         }
  105.         else {
  106.             $contents = $this->fetch($file);
  107.  
  108.             // Write the cache
  109.             if($fp = @fopen($this->cache_id, 'w')) {
  110.                 fwrite($fp, $contents);
  111.                 fclose($fp);
  112.             }
  113.             else {
  114.                 die('Unable to write cache.');
  115.             }
  116.  
  117.             return $contents;
  118.         }
  119.     }
  120. }
  121.  
  122.  
  123. /**
  124.  * Example of file-based template usage.  This uses two templates.
  125.  * Notice that the $bdy object is assigned directly to a $tpl var.
  126.  * The template system has built in a method for automatically
  127.  * calling the fetch() method of a passed in template.
  128.  */
  129. /*$tpl = & new Template();
  130. $tpl->set('title', 'My Test Page');
  131. $tpl->set('intro', 'The intro paragraph.');
  132. $tpl->set('list', array('cat', 'dog', 'mouse'));
  133.  
  134. $bdy = & new Template('body.tpl');
  135. $bdy->set('title', 'My Body');
  136. $bdy->set('footer', 'My Footer');
  137.  
  138. $tpl->set('body', $bdy);
  139.  
  140. echo $tpl->fetch('index.tpl');*/
  141.  
  142.  
  143. /**
  144.  * Example of cached template usage.  Doesn't provide any speed increase since
  145.  * we're not getting information from multiple files or a database, but it
  146.  * introduces how the is_cached() method works.
  147.  */
  148.  
  149. /**
  150.  * Define the template file we will be using for this page.
  151.  */
  152. #$file = 'index.tpl';
  153.  
  154. /**
  155.  * Pass a unique string for the template we want to cache.  The template
  156.  * file name + the server REQUEST_URI is a good choice because:
  157.  *    1. If you pass just the file name, re-used templates will all
  158.  *       get the same cache.  This is not the desired behavior.
  159.  *    2. If you just pass the REQUEST_URI, and if you are using multiple
  160.  *       templates per page, the templates, even though they are completely
  161.  *       different, will share a cache file (the cache file names are based
  162.  *       on the passed-in cache_id.
  163.  */
  164. #$tpl = & new CachedTemplate($file . $_SERVER['REQUEST_URI']);
  165.  
  166. /**
  167.  * Test to see if the template has been cached.  If it has, we don't
  168.  * need to do any processing.  Thus, if you put a lot of db calls in
  169.  * here (or file reads, or anything processor/disk/db intensive), you
  170.  * will significantly cut the amount of time it takes for a page to
  171.  * process.
  172.  */
  173. /*if(!($tpl->is_cached())) {
  174.     $tpl->set('title', 'My Title');
  175.     $tpl->set('intro', 'The intro paragraph.');
  176.     $tpl->set('list', array('cat', 'dog', 'mouse'));
  177. }*/
  178.  
  179. /**
  180.  * Fetch the cached template.  It doesn't matter if is_cached() succeeds
  181.  * or fails - fetch_cache() will fetch a cache if it exists, but if not,
  182.  * it will parse and return the template as usual (and make a cache for
  183.  * next time).
  184.  */
  185. #echo $tpl->fetch_cache($file);
  186. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement