simplify render

580-view-class-steroids
fadrian06 1 day ago
parent d28939f204
commit 206eff4f3d

@ -27,9 +27,6 @@ class View
/** @var array<string, mixed> View variables */ /** @var array<string, mixed> View variables */
protected array $vars = []; protected array $vars = [];
/** Template file. */
private string $template;
/** @param string $path Path to templates directory */ /** @param string $path Path to templates directory */
public function __construct(string $path = '.') public function __construct(string $path = '.')
{ {
@ -96,24 +93,25 @@ class View
*/ */
public function render(string $file, ?array $templateData = null): void public function render(string $file, ?array $templateData = null): void
{ {
$this->template = $this->getTemplate($file); $template = $this->getTemplate($file);
if (!$this->exists($file)) {
$normalized_path = $this::normalizePath($template);
if (!\file_exists($this->template)) {
$normalized_path = self::normalizePath($this->template);
throw new Exception("Template file not found: $normalized_path."); throw new Exception("Template file not found: $normalized_path.");
} }
\extract($this->vars); extract($this->vars);
if (\is_array($templateData) === true) { if (is_array($templateData)) {
\extract($templateData); extract($templateData);
if ($this->preserveVars === true) { if ($this->preserveVars) {
$this->vars = \array_merge($this->vars, $templateData); $this->vars += $templateData;
} }
} }
include $this->template; include $template;
} }
/** /**

Loading…
Cancel
Save