/** * Render the primary template, setting the variables for the first action and * redirects the user to the noroute template if necessary */ public static function render() { $engine = new Primitus_View_Engine(); $dispatcher = Primitus::registry('Dispatcher'); $action = $dispatcher->getFirstAction(); $moduleName = $dispatcher->formatControllerName($action->getControllerName()); $actionName = $dispatcher->formatActionName($action->getActionName()); $controller = new $moduleName(); $content_type = $controller->getContentType($action); header("Content-type: {$content_type}"); if ($moduleName == "IndexController" && $actionName == "norouteAction") { $view = Primitus_View::getInstance($moduleName); print $view->render($action->getActionName()); } else { switch (true) { case $controller instanceof ApplicationController: $engine->assign("__Primitus_Primary_Module_Name", $moduleName); $engine->assign("__Primitus_Primary_Module_Action", $actionName); $engine->display(self::MAIN_TEMPLATE); break; default: $view = Primitus_View::getInstance($moduleName); print $view->render($action->getActionName()); break; } } }
/** * The Primitus View plugin is a procedural function which implements the logic of * the template-level {render} function. It is responsible for either calling the * requested controller's render() method to get the template, or simply processing * the template in the views/ directory if the controller didn't exist. * * You can set template-level variables in the module being rendered by simply setting * them in the smarty {render} function. i.e. * * {render module="blog" action="view" entry=$entry} * * will expose the {$entry} template variable to the blog::view template in /blog/view.tpl * * @category Primitus * @package Primitus * @subpackage View * @copyright Copyright (c) 2006 John Coggeshall * @license http://framework.zend.com/license/new-bsd New BSD License */ function Primitus_View_Plugin_Render($params, &$smarty) { if (!isset($params['module'])) { throw new Primitus_View_Exception("No module name was provided to render"); } $module = $params['module']; if (strtolower(substr($module, strlen($module) - 10)) != "controller") { $module .= "Controller"; } $action = isset($params['action']) ? $params['action'] : "indexAction"; $dispatcher = Primitus::registry('Dispatcher'); $controllerFile = $dispatcher->formatControllerFile($module); if (file_exists($controllerFile)) { // Load the Class Zend::loadClass($module, $dispatcher->getControllerDirectory()); $controller = new $module(); if ($controller instanceof Primitus_Controller_Action_Base) { unset($params['module']); unset($params['action']); if (!empty($params)) { $view = Primitus_View::getInstance($module); foreach ($params as $key => $value) { $view->assign($key, $value); } } return $controller->render($action); } else { throw new Primitus_View_Exception("Bad Request"); } } else { $view = Primitus_View::getInstance($module); unset($params['module']); unset($params['action']); if (!empty($params)) { $view = Primitus_View::getInstance($module); foreach ($params as $key => $value) { $view->assign($key, $value); } } return $view->render($action); } }
/** * Default render() implementation, which renders the template automatically * based on class/action provided * * @param string $actionName The qualified action name being rendered * @return string The template fully rendered */ public function render($actionName) { $view = Primitus_View::getInstance(get_class($this)); if (empty($actionName)) { $actionName = "indexAction"; } if (array_key_exists($actionName, $this->_viewMap)) { $actionName = $this->_viewMap[$actionName]; } return $view->render($actionName); }
/** * Returns an instance of the view for a particular scope * * @param string $scopeName The qualified controller name * @return Primitus_View the instance of the view for this particular scope */ public static final function getInstance($scopeName) { $scopeName = strtolower($scopeName); if (!is_array(self::$_instance)) { self::$_instance = array(); } if (!isset(self::$_instance[$scopeName]) || !self::$_instance[$scopeName] instanceof self) { self::$_instance[$scopeName] = new self(); self::$_instance[$scopeName]->setScopeName($scopeName); } return self::$_instance[$scopeName]; }
/** * The Primitus Dispatcher * * @param Zend_Controller_Dispatcher_Token $action The action to dispatch * @param boolean $performDispatch Should the dispatch be performed, or just test * @return mixed If testing, true/false to if the operation would succeed, if not the next token or null */ protected function _dispatch(Zend_Controller_Dispatcher_Token $action, $performDispatch = false) { if (is_null($this->_directory)) { throw new Primitus_Controller_Dispatcher_Exception("Controller directory never set. Use setControllerDirectory() first."); } $controllerName = ($controllerName = $action->getControllerName()) ? $controllerName : 'index'; $controllerName = $this->formatControllerName($controllerName); $controllerFile = $this->formatControllerFile($controllerName); $actionName = $this->formatActionname(($actionName = $action->getActionName()) ? $actionName : 'index'); // Does the Controller exist? if (file_exists($controllerFile)) { // Load the Class Zend::loadClass($controllerName, $this->_directory); $controller = new $controllerName(); if ($controller instanceof Zend_Controller_Action) { if (method_exists($controller, $actionName)) { if (!$performDispatch) { return true; } return $this->_runController($controller, $action); } } else { throw new Primitus_Controller_Dispatcher_Exception("Bad Request"); } } /** * If the Class file doesn't exist, we check to see if a corresponding * template for the controller exists. If it does, we create a dummy controller * to handle the request of displaying the template. */ $view = Primitus_View::getInstance($controllerName); $templateFile = $view->getTemplatePathByAction($actionName); if (!$performDispatch) { return file_exists($templateFile); } $this->_firstAction = is_null($this->_firstAction) ? $action : $this->_firstAction; }