Advertisement
bueddl

Untitled

Sep 26th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.28 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  *
  5.  *
  6.  * @author Sebastian Büttner <sebastian.buettner@powerserverplus.net>
  7.  * @package void
  8.  * @version 1.0.0
  9.  * @copyright (c) 2013, powerserverplus.net
  10.  */
  11.  
  12. class FileNotFoundException extends Exception {}
  13.  
  14. class Template
  15. {
  16.     /**
  17.      * @var String Path to tempalte dir
  18.      */
  19.     const template_dir = 'templates';
  20.  
  21.     /**
  22.      * @var String (sub)template filename
  23.      */
  24.     protected $template = '';
  25.  
  26.     /**
  27.      * @var Array data to be used within the template
  28.      */
  29.     protected $data;
  30.  
  31.     /**
  32.      * Template constructor
  33.      *
  34.      * @param String $templ name of the template
  35.      */
  36.     public function __construct($templ, $layout = 'index')
  37.     {
  38.         $this->load($layout);
  39.  
  40.         $this->data = array();
  41.         $this->assign('subtemplate', $templ);
  42.     }
  43.  
  44.     /**
  45.      * Save a passed variable for later use in template
  46.      *
  47.      * @param String $name name of the varaible used to identify
  48.      * @param Mixed $value value to be assigned to the variable
  49.      */
  50.     public function assign($name, $value)
  51.     {
  52.         /* Separate name by dot */
  53.         $path = explode('.', $name);
  54.  
  55.         /* Take the last element of the array, as this is our final identifier */
  56.         $lastKey = array_pop($path);
  57.  
  58.         /* Create / fill array structure, start with the template`s data base array */
  59.         $parent = &$this->data;
  60.  
  61.         /* Iterate through the given name structure... */
  62.         foreach ($path as $key)
  63.         {
  64.             /* ...And create substructres where needed */
  65.             if (!isset($parent[$key]))
  66.                 $parent[$key] = array();
  67.  
  68.             /* Move the parent `pointer` deeper within the structure */
  69.             $parent = &$parent[$key];
  70.         }
  71.  
  72.         /* Finally assign the value to the deepest element in structure */
  73.         $parent[$lastKey] = $value;
  74.     }
  75.  
  76.     /**
  77.      * Prepare and display the tempalte
  78.      *
  79.      */
  80.     public function display()
  81.     {
  82.         /* We start with no code */
  83.         $template_code = '';
  84.  
  85.         /* Merge template data with session-flash data */
  86.         $data = $this->data;
  87.  
  88.         if (isset($_SESSION['appends']) && is_array($_SESSION['appends']) && !empty($_SESSION['appends']))
  89.             $data = array_merge($data, $_SESSION['appends']);
  90.  
  91.         $_SESSION['appends'] = array();
  92.  
  93.         /* And iterate then over all passed variables, making them executable code again */
  94.         foreach ($data as $key => $value)
  95.         {
  96.             /* Add a new line to the template code */
  97.             $template_code .=
  98.                 /* Write varaible name */
  99.                 '$' . $key . ' = ' .
  100.                 /* Let php unserialize the variables content (which has been serialized above) at runtime of evaluation */
  101.                 'unserialize(\'' .
  102.                 /* Serialize the variables content, what is needed for arrays */
  103.                 serialize($value) .
  104.                 /* Close unserialize function and do a newline for more proper readable code */
  105.                 '\');' . "\n";
  106.  
  107.         }
  108.  
  109.         /* Put a php close tag, as we are continuing with out html-code-template */
  110.         $template_code .= '?>';
  111.  
  112.         /* Append the template */
  113.         $template_code .= file_get_contents($this->template);
  114.  
  115.         /* Execute the build string as PHP-Code */
  116.         eval($template_code);
  117.     }
  118.  
  119.     /**
  120.      * Includes a given template by name
  121.      *
  122.      * @param String $templ the name of the tempalte file to be included
  123.      */
  124.     static function getInclude($templ)
  125.     {
  126.         /* Build the full path of the file to load */
  127.         $filename = Template::template_dir . '/' . $templ . '.template.php';
  128.  
  129.         /* Check whether the requested file does exist, if not throw exception */
  130.         if ( ! file_exists($filename) )
  131.             throw new FileNotFoundException('Template file ' . $filename . ' not found.');
  132.  
  133.         /* return the filename to include */
  134.         return $filename;
  135.     }
  136.  
  137.     /**
  138.      * Helper function, gives $default back when $var is unset
  139.      *
  140.      * @param Mixed $var
  141.      * @param Mixed $default
  142.      *
  143.      */
  144.     static function withDefault(&$var, $default)
  145.     {
  146.         if (!isset($var))
  147.             return $default;
  148.        
  149.         return $var;
  150.     }
  151.  
  152.     /**
  153.      * Try to load template by name, can throw FileNotFoundException
  154.      *
  155.      * @param String $templ template name
  156.      */
  157.     private function load($templ)
  158.     {
  159.         /* Build the full path of the file to load */
  160.         $filename = Template::template_dir . '/' . $templ . '.template.php';
  161.  
  162.         /* Check whether the requested file does exist, if not throw exception */
  163.         if ( ! file_exists($filename) )
  164.             throw new FileNotFoundException('Template file ' . $filename . ' not found.');
  165.  
  166.         /* Set the filename of the template */
  167.         $this->template = $filename;
  168.     }
  169.  
  170.  
  171. }
  172.  
  173. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement