TemplateView
[ class tree: TemplateView ] [ index: TemplateView ] [ all elements ]

Source for file TemplateView.php

Documentation is available at TemplateView.php

  1. <?php
  2. /**
  3.  * MVClasses
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * @category    MVClasses
  14.  * @package     TemplateView
  15.  * @copyright   Copyright (c) 2007 Sven Strittmatter
  16.  */
  17.  
  18. /**
  19.  * Enter description here...
  20.  *
  21.  * Longdesc
  22.  *
  23.  * @todo complete description
  24.  *
  25.  * @category    MVClasses
  26.  * @package     TemplateView
  27.  * @copyright     Copyright (c) 2007 Sven Strittmatter
  28.  * @author    "Sven Strittmatter" <ausserirdisch@sven-space.de>
  29.  * @version     1.0
  30.  * @link        http://www.sven-space.de/code/php/MVClasses
  31.  */
  32. class TemplateView {
  33.     private $_template;
  34.     private $_vars;
  35.     private $_helpers;
  36.  
  37.     public function __construct($template{
  38.         $this->_template $template;
  39.         $this->_vars array();
  40.         $this->_helpers array();
  41.     }
  42.  
  43.     public function assign($name$value{
  44.         $this->_vars[$name$value;
  45.     }
  46.  
  47.     public function render(Request $reqResponse $res{
  48.         ob_start();
  49.         include_once $this->_template;
  50.         $res->write(ob_get_clean());
  51.     }
  52.  
  53.     public function __get($name{
  54.         if (isset($this->_vars[$name])) {
  55.             return $this->_vars[$name];
  56.         }
  57.  
  58.         return null;
  59.     }
  60.  
  61.     public function __call($name$args{
  62.         $helper $this->_loadViewHelper($name);
  63.  
  64.         if (null === $helper{
  65.             throw new Exception("Unknown view helper: $name");
  66.         }
  67.  
  68.         return $helper->execute($args);
  69.     }
  70.  
  71.     protected function _loadViewHelper($helper{
  72.         $name ucfirst($helper);
  73.  
  74.         if (!isset($this->_helpers[$helper])) {
  75.             $class $name."ViewHelper";
  76.  
  77.             $file dirname($this->_template)."/ViewHelpers/{$class}.php";
  78.  
  79.             if (!file_exists($file)) {
  80.                 return null;
  81.             }
  82.  
  83.             include_once $file;
  84.             $this->_helpers[$helpernew $class();
  85.         }
  86.  
  87.         return $this->_helpers[$helper];
  88.     }
  89. }

Documentation generated on Sun, 02 Aug 2009 17:15:04 +0200 by phpDocumentor 1.4.2