Exemplo n.º 1
0
 public function setContentWithLayout(string $textContent, string $layout = 'Global', array $vars = [])
 {
     $this->compiler->setContents($textContent);
     if ($path = $this->resolver->getView("Layout\\{$layout}")) {
         $textContent = $this->compiler->compile([$path]);
     }
     $this->setVars($vars);
     $this->setContent($textContent);
     return $this;
 }
Exemplo n.º 2
0
 /**
  * @param string $viewFile
  * @param bool $withLayout
  * @param bool $useCache
  *
  * @return ViewParser
  * @throws ViewError
  */
 public function loadViewFile(string $viewFile, bool $withLayout = true, bool $useCache = true)
 {
     $compiler = function () use($viewFile, $withLayout) {
         if ($files[] = $path = $this->resolver->getView($viewFile)) {
             if ($withLayout === true) {
                 for ($i = 0, $layout = dirname($viewFile); $i <= 10 && $layout !== '.'; $layout = dirname($layout), $i++) {
                     $includes[] = $layout;
                 }
                 $includes = array_merge($includes ?? [], ['Global']);
             }
             if ($additionalLayouts = $this->getAdditionalLayoutFiles()) {
                 $includes = array_merge($includes ?? [], $additionalLayouts);
             }
             if (!empty($includes)) {
                 $files = array_merge($files, array_filter(array_map(function ($l) {
                     return $this->resolver->getView("Layout\\{$l}");
                 }, $includes)));
             }
             $compiled = $this->compiler->compile($files);
             return $compiled;
         } else {
             throw new ViewError("Unable to find view file: {$viewFile}");
         }
     };
     $this->content = $useCache ? $this->cache->get("compiled:{$viewFile}", $compiler, self::CACHE_TIMEOUT) : $compiler();
     return $this;
 }
Exemplo n.º 3
0
 private function loadTemplateFromUrl($templateUrl)
 {
     if (!empty($templateUrl)) {
         if ($path = (new Resolver())->getView("Helper\\{$templateUrl}")) {
             $compiler = new Compiler();
             //not using DI since Helper object is often created by user
             $this->template = $compiler->compile([$path]);
         } else {
             throw new ViewError(sprintf("Unable to find helper: %s", $templateUrl));
         }
     }
 }