ManiaLib 1.0b1 API Reference

Source for file View.class.php

Documentation is available at View.class.php

  1. <?php
  2. /**
  3.  * @author Maxime Raoust
  4.  * @copyright 2009-2010 NADEO
  5.  * @package ManiaLib
  6.  * @subpackage MVC
  7.  */
  8.  
  9. /**
  10.  * View rendering features
  11.  * @package ManiaLib
  12.  * @subpackage MVC
  13.  */
  14. abstract class View
  15. {
  16.     /**
  17.      * Renders a view
  18.      * You can use this method from within a view to render another view.
  19.      * Usefull for things like rendering a navigation menu on every page.
  20.      * 
  21.      * Examples:
  22.      * <code>
  23.      * View::render('header'); // Renders /views/header.php
  24.      * View::render('Home', '_navigation'); // Renders /views/Home/_navigation.php
  25.      * </code>
  26.      */
  27.     public static function render($controllerName$actionName=null)
  28.     {
  29.         $viewFilename self::getFilename($controllerName,$actionName);
  30.         if(!file_exists($viewFilename))
  31.         {
  32.             $viewFilename self::getFilename($controllerName$actionName
  33.                 APP_MVC_FRAMEWORK_VIEWS_PATH);
  34.             if(!file_exists($viewFilename))
  35.             {
  36.                 throw new ViewNotFoundException($controllerName.'::'.$actionName);
  37.             }
  38.         }
  39.         $response ResponseEngine::getInstance();
  40.         $request RequestEngineMVC::getInstance();
  41.         ob_start();
  42.         require($viewFilename);
  43.         $response->appendBody(ob_get_contents());
  44.         ob_end_clean();
  45.     }
  46.     
  47.     /**
  48.      * @ignore
  49.      */
  50.     public static function getFilename($controllerName$actionName=null$path=APP_MVC_VIEWS_PATH)
  51.     {
  52.         if($controllerName && $actionName)
  53.         {
  54.             return $path.$controllerName.'/'.$actionName.'.php';
  55.         }
  56.         else
  57.         {
  58.             return $path.$controllerName.'.php';
  59.         }
  60.     }
  61. }
  62.  
  63. /**
  64.  * @package ManiaLib
  65.  * @subpackage MVC
  66.  * @ignore
  67.  */
  68. class ViewNotFoundException extends MVCException {}
  69.  
  70. ?>