|
|
|
@ -92,6 +92,17 @@ class View
|
|
|
|
* @throws Exception If template not found
|
|
|
|
* @throws Exception If template not found
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function render(string $file, ?array $templateData = null): void
|
|
|
|
public function render(string $file, ?array $templateData = null): void
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
echo $this->fetch($file, $templateData);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Gets the output of a template
|
|
|
|
|
|
|
|
* @param string $file Template file
|
|
|
|
|
|
|
|
* @param ?array<string, mixed> $data Template data
|
|
|
|
|
|
|
|
* @return string Output of template
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function fetch(string $file, ?array $data = null): string
|
|
|
|
{
|
|
|
|
{
|
|
|
|
$template = $this->getTemplate($file);
|
|
|
|
$template = $this->getTemplate($file);
|
|
|
|
|
|
|
|
|
|
|
|
@ -101,30 +112,26 @@ class View
|
|
|
|
|
|
|
|
|
|
|
|
extract($this->vars);
|
|
|
|
extract($this->vars);
|
|
|
|
|
|
|
|
|
|
|
|
if (is_array($templateData)) {
|
|
|
|
if (is_array($data)) {
|
|
|
|
extract($templateData);
|
|
|
|
extract($data);
|
|
|
|
|
|
|
|
|
|
|
|
if ($this->preserveVars) {
|
|
|
|
if ($this->preserveVars) {
|
|
|
|
$this->vars += $templateData;
|
|
|
|
$this->vars += $data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
include $template;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Gets the output of a template
|
|
|
|
|
|
|
|
* @param string $file Template file
|
|
|
|
|
|
|
|
* @param ?array<string, mixed> $data Template data
|
|
|
|
|
|
|
|
* @return string Output of template
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function fetch(string $file, ?array $data = null): string
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
ob_start();
|
|
|
|
|
|
|
|
include $template;
|
|
|
|
|
|
|
|
$view = ob_get_clean();
|
|
|
|
|
|
|
|
preg_match('/<f-(?<component>[a-z-]+)\s*\/>/', $view, $matches);
|
|
|
|
|
|
|
|
|
|
|
|
$this->render($file, $data);
|
|
|
|
if ($matches) {
|
|
|
|
|
|
|
|
$tag = $matches[0];
|
|
|
|
|
|
|
|
$component = $this->fetch("components/{$matches['component']}");
|
|
|
|
|
|
|
|
$view = str_replace($tag, $component, $view);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ob_get_clean();
|
|
|
|
return $view;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
|