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; }
/** * @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; }