Classe View by Sahid add there is 0 minute(s)
  1. <?php
  2. class View
  3. {
  4.     private $_path;     
  5.         private $_vars;
  6.        
  7.         /**
  8.          * Constructeur de la classe
  9.          *
  10.          * @param unknown_type $path
  11.          */
  12.         public function __construct ($path)
  13.         {
  14.                 $this->_path = $this->createPath($path);
  15.                 $this->_vars = array ();
  16.         }
  17.  
  18.         /**
  19.          * Admet une varriable cree a la volé
  20.          *
  21.          * @param string $key
  22.          * @param mix $val
  23.          */
  24.         public function __set ($key, $val)
  25.         {
  26.                 $this->_vars[$key] = $val;
  27.         }
  28.        
  29.         /**
  30.          * Retourne la valeur cree par le __set() magique
  31.          *
  32.          * @param string $key
  33.          * @return mix
  34.          */
  35.         public function __get ($key)
  36.         {
  37.                 return $this->_vars[$key];
  38.         }
  39.        
  40.         /**
  41.          * Netoie les valeurs en sortie
  42.          *
  43.          * @param string $var
  44.          * @return string
  45.          */
  46.         public function escape ($var)
  47.         {
  48.             return htmlspecialchars ($var);
  49.         }
  50.        
  51.         /**
  52.          * Cree le chemin d'accee des templates
  53.          *
  54.          * @param string $path
  55.          * @return string
  56.          */
  57.         protected function createPath ($path)
  58.         {
  59.            $dir = rtrim ($path, "\\/" . DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  60.            return $dir;
  61.         }
  62.        
  63.         /**
  64.          * Cree le chemin d'acce complet
  65.          * ver le fichier
  66.          *
  67.          * @param string $file
  68.          * @return string
  69.          */
  70.         protected function createContent ($file)
  71.         {
  72.             $full = $this->_path . $file;
  73.             if (is_readable($full))
  74.                return $this->_path . $file;
  75.             else throw new Exception("<b>{$file}</b> n'est pas present dans {$this->_path}");
  76.         }
  77.        
  78.         /**
  79.          * Rendu d'un fichier
  80.          *
  81.          * @param mix $file
  82.          * @return string
  83.          */
  84.         public function render ($file)
  85.         {
  86.                 $content = $this->createContent ($file);
  87.                
  88.                 ob_start();
  89.                 include ($content);
  90.                
  91.                 return ob_get_clean();
  92.         }
  93. }