Source for file TemplateView.php

Documentation is available at TemplateView.php

  1. <?php
  2.  
  3. /**
  4.  * @package inc
  5.  * @subpackage view
  6.  */
  7.  
  8. /**
  9.  *
  10.  * TemplateView class
  11.  *
  12.  * @author Felix Rupp <kontakt@felixrupp.com>
  13.  * @version $Id$
  14.  * @copyright Copyright (c) 2011, Felix Rupp, Nicole Reinhardt
  15.  * @license http://www.opensource.org/licenses/mit-license.php MIT-License
  16.  * @license http://www.gnu.org/licenses/gpl.html GNU GPL
  17.  * 
  18.  * @package inc
  19.  * @subpackage view
  20.  */
  21. class TemplateView {
  22.     
  23.     # Attributes
  24.     /**
  25.      * @var array $values Will contain all template-values
  26.      */
  27.     private $_values;
  28.  
  29.     /**
  30.      * @var String Path to active template directory
  31.      */
  32.     private $_activeTemplate;
  33.     
  34.     /**
  35.      * @var String Name of the template file to use. DEFAULT ist index
  36.      */
  37.     private $_templateFile;
  38.     
  39.     /**
  40.      * @var array Array with additional CSS files to load
  41.      */
  42.     private $_additionalCssFiles array();
  43.     
  44.     /**
  45.      * @var array Array with additional JS files to load
  46.      */
  47.     private $_additionalJsFiles array();
  48.     
  49.     
  50.     
  51.     /**
  52.      * Constructor of TemplateView
  53.      * 
  54.      * @param String $activeTemplate Path to active template directory
  55.      * @return void 
  56.      */
  57.     public function TemplateView($activeTemplate{
  58.         
  59.         $this->_activeTemplate $activeTemplate;
  60.         
  61.         $this->_templateFile "index";
  62.         
  63.         $this->_values array();
  64.         
  65.     // End of constructor declaration
  66.     
  67.     
  68.     
  69.     /**
  70.      * Method to assign all template values to our template
  71.      * 
  72.      * @param String $key Array-key
  73.      * @param String $value Array-value for key
  74.      * @return void 
  75.      */
  76.     public function assign($key$value{
  77.         
  78.         $this->_values[$key$value;
  79.         
  80.     // End of method declaration
  81.     
  82.     
  83.     
  84.     /**
  85.      * Method to add correct JS and CSS files from template
  86.      * 
  87.      * @return String with html content of JS and CSS files
  88.      */
  89.     public function addCssJs({
  90.         
  91.         $cssJs "<link rel=\"shortcut icon\" href=\"".TEMPLATEDIR."/".$this->_activeTemplate."/images/favicon.ico\" type=\"image/ico\">\n";
  92.         $cssJs .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"".TEMPLATEDIR."/".$this->_activeTemplate."/css/styles.css\">\n";
  93.         
  94.         foreach($this->_additionalCssFiles as $cssFile{
  95.             $cssJs .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"".TEMPLATEDIR."/".$this->_activeTemplate."/css/".$cssFile.".css\">\n";
  96.         }
  97.         
  98.         $cssJs .= "<script type=\"text/javascript\" src=\"".TEMPLATEDIR."/".$this->_activeTemplate."/js/jquery.min.js\"></script>\n";
  99.         
  100.         $cssJs .= "<script type=\"text/javascript\" src=\"".TEMPLATEDIR."/".$this->_activeTemplate."/js/scripts.js\"></script>\n";
  101.         
  102.         foreach($this->_additionalJsFiles as $jsFile{
  103.             $cssJs .= "<script type=\"text/javascript\" src=\"".TEMPLATEDIR."/".$this->_activeTemplate."/js/".$jsFile.".js\"></script>\n";
  104.         }
  105.         
  106.         return $cssJs;
  107.         
  108.     // End of method declaration
  109.     
  110.     
  111.     
  112.     /**
  113.      * Method to set different template filename
  114.      * 
  115.      * @param String $templateFile 
  116.      */
  117.     public function setTemplateFile($templateFile{
  118.         
  119.         $this->_templateFile $templateFile;
  120.         
  121.     // End of method declaration
  122.     
  123.     
  124.     
  125.     /**
  126.      * Function which renders the template
  127.      * 
  128.      * @return void 
  129.      */
  130.     public function render({
  131.         
  132.         if($this->_values{
  133.             foreach($this->_values as $key => $val{
  134.                 $$key $val;
  135.             }
  136.         }
  137.         
  138.         $this->_templateFile "index";
  139.         
  140.         include BASEDIR.'/template/'.$this->_activeTemplate.'/'.$this->_templateFile.'.tpl.php';
  141.         
  142.     // End of method declaration
  143.     
  144.     
  145.     
  146.     /**
  147.     * Method to render the template with gzip compression
  148.     *
  149.     * @return void 
  150.     */
  151.     public function renderGzipped({
  152.     
  153.         ob_start();
  154.         ob_implicit_flush(0);
  155.     
  156.         $this->render();
  157.     
  158.         global $HTTP_ACCEPT_ENCODING;
  159.     
  160.         if(headers_sent()){
  161.                 
  162.             $encoding false;
  163.                 
  164.         elseifstrpos($HTTP_ACCEPT_ENCODING'x-gzip'!== false {
  165.                 
  166.             $encoding 'x-gzip';
  167.                 
  168.         elseifstrpos($HTTP_ACCEPT_ENCODING,'gzip'!== false {
  169.                 
  170.             $encoding 'gzip';
  171.                 
  172.         else {
  173.                 
  174.             $encoding false;
  175.                 
  176.         }
  177.     
  178.         if($encoding{
  179.                 
  180.             $contents ob_get_contents();
  181.                 
  182.             ob_end_clean();
  183.                 
  184.             header('Content-Encoding: '.$encoding);
  185.                 
  186.             print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
  187.                 
  188.             $size strlen($contents);
  189.                 
  190.             $contents gzcompress($contents9);
  191.                 
  192.             $contents substr($contents0$size);
  193.                 
  194.             print($contents);
  195.                 
  196.         else {
  197.                 
  198.             ob_end_flush();
  199.     
  200.         }
  201.     
  202.     // End of method declaration
  203.     
  204.     
  205.     
  206.     /**
  207.      * Method to return template content as string
  208.      * 
  209.      * @return Content of templatefile
  210.      */
  211.     public function returnTemplate({
  212.         
  213.         if($this->_values{
  214.             foreach($this->_values as $key => $val{
  215.                 $$key $val;
  216.             }
  217.         }
  218.         
  219.         return file_get_contents(TEMPLATEDIR.'/'.$this->_activeTemplate.'/'.$this->_templateFile.'.tpl.php');
  220.         
  221.     // End of method declaration
  222.     
  223.     
  224.     
  225.     /**
  226.      * Method to return activeTemplate-path
  227.      * 
  228.      * @return activeTemplate-path 
  229.      */
  230.     public function getActiveTemplate({
  231.         return $this->_activeTemplate;
  232.     // End of method declaration
  233.     
  234.     
  235.     
  236.     /**
  237.      * Method to add new CSS files to array
  238.      * 
  239.      * @param String $cssFilename 
  240.      */
  241.     public function addCssFile($cssFilename{
  242.         array_push($this->_additionalCssFiles$cssFilename);
  243.     // End of method declaration
  244.     
  245.     /**
  246.     * Method to add new JS files to array
  247.     *
  248.     * @param String $jsFilename 
  249.     */
  250.     public function addJsFile($jsFilename{
  251.         array_push($this->_additionalJsFiles$jsFilename);
  252.     // End of method declaration
  253.  
  254.     
  255. }
  256. ?>