예제 #1
0
파일: View.php 프로젝트: edrdesigner/awf
 /**
  * Loads a template given any path. The path is in the format:
  * viewname/templatename
  *
  * @param   string $path        The template path
  * @param   array  $forceParams A hash array of variables to be extracted in the local scope of the template file
  *
  * @return  string  The output of the template
  *
  * @throws  \Exception  When the layout file is not found
  */
 public function loadAnyTemplate($path = '', $forceParams = array())
 {
     $template = \Awf\Application\Application::getInstance()->getTemplate();
     $layoutTemplate = $this->getLayoutTemplate();
     // Parse the path
     $templateParts = $this->parseTemplatePath($path);
     // Get the default paths
     $templatePath = $this->container->templatePath;
     $paths = array();
     $paths[] = $templatePath . '/' . $template . '/html/' . $this->input->getCmd('option', '') . '/' . $templateParts['view'];
     $paths[] = $this->container->basePath . '/views/' . $templateParts['view'] . '/tmpl';
     $paths[] = $this->container->basePath . '/View/' . $templateParts['view'] . '/tmpl';
     $paths = array_merge($paths, $this->templatePaths);
     // Look for a template override
     if (isset($layoutTemplate) && $layoutTemplate != '_' && $layoutTemplate != $template) {
         $apath = array_shift($paths);
         array_unshift($paths, str_replace($template, $layoutTemplate, $apath));
     }
     $filetofind = $templateParts['template'] . '.php';
     $this->_tempFilePath = \Awf\Utils\Path::find($paths, $filetofind);
     if ($this->_tempFilePath) {
         // Unset from local scope
         unset($template);
         unset($layoutTemplate);
         unset($paths);
         unset($path);
         unset($filetofind);
         // Never allow a 'this' property
         if (isset($this->this)) {
             unset($this->this);
         }
         // Force parameters into scope
         if (!empty($forceParams)) {
             extract($forceParams);
         }
         // Start capturing output into a buffer
         ob_start();
         // Include the requested template filename in the local scope
         // (this will execute the view logic).
         include $this->_tempFilePath;
         // Done with the requested template; get the buffer and
         // clear it.
         $this->output = ob_get_contents();
         ob_end_clean();
         return $this->output;
     } else {
         return new \Exception(\Awf\Text\Text::sprintf('AWF_APPLICATION_ERROR_LAYOUTFILE_NOT_FOUND', $path), 500);
     }
 }
예제 #2
0
 /**
  * Returns a named View object
  *
  * @param   string $name     The Model name. If null we'll use the modelName
  *                           variable or, if it's empty, the same name as
  *                           the Controller
  * @param   array  $config   Configuration parameters to the Model. If skipped
  *                           we will use $this->config
  *
  * @return  View  The instance of the Model known to this Controller
  */
 public function getView($name = null, $config = array())
 {
     if (!empty($name)) {
         $viewName = strtolower($name);
     } elseif (!empty($this->viewName)) {
         $viewName = strtolower($this->viewName);
     } else {
         $viewName = strtolower($this->view);
     }
     if (!array_key_exists($viewName, $this->viewInstances)) {
         $appName = $this->container->application->getName();
         if (empty($config)) {
             $config = $this->config;
         }
         $viewType = $this->input->getCmd('format', 'html');
         $this->container['mvc_config'] = $config;
         $this->viewInstances[$viewName] = View::getInstance($appName, $viewName, $viewType, $this->container);
     }
     return $this->viewInstances[$viewName];
 }