Exemplo n.º 1
0
 /**
  * Loads the view with the specified name.
  * @param string $name The view name.
  * @return string The view contents.
  * @throws InvalidCallException Unable to locate the view file.
  */
 public function load($name) : string
 {
     if (!isset($this->views[$name])) {
         $viewRenderer = $this->getViewRenderer();
         $cacheId = $viewRenderer->getCacheId();
         $cache = mb_strlen($cacheId) ? \Yii::$app->get($cacheId) : null;
         $cacheKey = static::CACHE_KEY_PREFIX . ":{$name}";
         if ($cache && $cache->exists($cacheKey)) {
             $output = $cache[$cacheKey];
         } else {
             $path = FileHelper::localize($this->findViewFile($name));
             if (!is_file($path)) {
                 throw new InvalidCallException("The view file \"{$path}\" does not exist.");
             }
             $output = @file_get_contents($path);
             if ($cache) {
                 $cache->set($cacheKey, $output, $viewRenderer->getCachingDuration());
             }
         }
         $this->views[$name] = $output;
     }
     return $this->views[$name];
 }
Exemplo n.º 2
0
 /**
  * Renders a view file.
  * @param \yii\base\View $view The view object used for rendering the file.
  * @param string $file The view file.
  * @param array $params The parameters to be passed to the view file.
  * @return string The rendering result.
  * @throws InvalidCallException The specified view file is not found.
  */
 public function render($view, $file, $params) : string
 {
     $cacheId = $this->getCacheId();
     $cache = mb_strlen($cacheId) ? \Yii::$app->get($cacheId) : null;
     $cacheKey = static::CACHE_KEY_PREFIX . ":{$file}";
     if ($cache && $cache->exists($cacheKey)) {
         $output = $cache[$cacheKey];
     } else {
         $path = FileHelper::localize($file);
         if (!is_file($path)) {
             throw new InvalidCallException("View file \"{$file}\" does not exist.");
         }
         $output = @file_get_contents($path);
         if ($cache) {
             $cache->set($cacheKey, $output, $this->getCachingDuration());
         }
     }
     $values = ArrayHelper::merge(['this' => $view], is_array($params) ? $params : []);
     return $this->engine->render($output, $values);
 }