Example #1
0
 /**
  * Renders a view file.
  *
  * If [[theme]] is enabled (not null), it will try to render the themed version of the view file as long
  * as it is available.
  *
  * The method will call [[FileHelper::localize()]] to localize the view file.
  *
  * If [[renderers|renderer]] is enabled (not null), the method will use it to render the view file.
  * Otherwise, it will simply include the view file as a normal PHP file, capture its output and
  * return it as a string.
  *
  * @param string $viewFile the view file. This can be either an absolute file path or an alias of it.
  * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file.
  * @param object $context the context that the view should use for rendering the view. If null,
  *        existing [[context]] will be used.
  * @return string the rendering result
  * @throws InvalidParamException if the view file does not exist
  */
 public function renderFile($viewFile, $params = [], $context = null)
 {
     $viewFile = Leaps::getAlias($viewFile);
     if ($this->theme !== null) {
         $viewFile = $this->theme->applyTo($viewFile);
     }
     if (is_file($viewFile)) {
         $viewFile = FileHelper::localize($viewFile);
     } else {
         throw new InvalidParamException("The view file does not exist: {$viewFile}");
     }
     $oldContext = $this->context;
     if ($context !== null) {
         $this->context = $context;
     }
     $output = '';
     $this->_viewFiles[] = $viewFile;
     if ($this->beforeRender($viewFile, $params)) {
         Leaps::trace("Rendering view file: {$viewFile}", __METHOD__);
         $ext = pathinfo($viewFile, PATHINFO_EXTENSION);
         if (isset($this->renderers[$ext])) {
             if (is_array($this->renderers[$ext]) || is_string($this->renderers[$ext])) {
                 $this->renderers[$ext] = Leaps::createObject($this->renderers[$ext]);
             }
             /* @var $renderer ViewRenderer */
             $renderer = $this->renderers[$ext];
             $output = $renderer->render($this, $viewFile, $params);
         } else {
             $output = $this->renderPhpFile($viewFile, $params);
         }
         $this->afterRender($viewFile, $params, $output);
     }
     array_pop($this->_viewFiles);
     $this->context = $oldContext;
     return $output;
 }