89 lines
No EOL
2.4 KiB
PHP
89 lines
No EOL
2.4 KiB
PHP
<?php
|
|
|
|
|
|
namespace Venom\Views;
|
|
|
|
|
|
use Venom\Core\ArgumentHandler;
|
|
use Venom\Core\Config;
|
|
use Venom\Venom;
|
|
|
|
class VenomRenderer
|
|
{
|
|
private Venom $venom;
|
|
private ?RenderController $controller;
|
|
private string $templateData = '';
|
|
private array $vars = [];
|
|
private string $baseTemplate = '';
|
|
private string $templateDir = '';
|
|
private string $assetsDir = '';
|
|
|
|
public function __construct(Venom $venom)
|
|
{
|
|
$this->venom = $venom;
|
|
}
|
|
|
|
public function render(): void
|
|
{
|
|
$isAsync = false;
|
|
if ($this->controller) {
|
|
ob_start();
|
|
$this->controller->render($this);
|
|
$this->templateData = ob_get_clean();
|
|
$isAsync = $this->controller->getTemplate() === 'async';
|
|
}
|
|
if ($isAsync || ArgumentHandler::get()->getItem('async', 'false') === 'true') {
|
|
echo $this->templateData;
|
|
exit;
|
|
}
|
|
$this->loadBasicTemplate();
|
|
}
|
|
|
|
public function loadBasicTemplate(): void
|
|
{
|
|
if (file_exists($this->templateDir . $this->baseTemplate)) {
|
|
include_once $this->templateDir . $this->baseTemplate;
|
|
} else {
|
|
echo 'Base Template not found...';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* function will load a template (without extension!) into a variable and return the content
|
|
* @param $template
|
|
* @param string $varName
|
|
* @return false|string
|
|
*/
|
|
public function includeTemplate($template, $varName = '')
|
|
{
|
|
$template .= '.php';
|
|
if (file_exists($this->templateDir . $template)) {
|
|
ob_start();
|
|
include_once $this->templateDir . $template;
|
|
$data = ob_get_clean();
|
|
$this->vars[$varName] = $data;
|
|
return $data;
|
|
}
|
|
$this->vars[$varName] = '';
|
|
return '';
|
|
}
|
|
|
|
public function addVar($name, $value): void
|
|
{
|
|
$this->vars[$name] = $value;
|
|
}
|
|
|
|
public function getVar($name)
|
|
{
|
|
return $this->vars[$name];
|
|
}
|
|
|
|
public function init(?RenderController $controller): void
|
|
{
|
|
$this->controller = $controller;
|
|
$data = Config::getInstance()->getRenderer();
|
|
$this->baseTemplate = $data->baseFile . '.php' ?? 'base.php';
|
|
$this->templateDir = __DIR__ . '/../../../tpl/' . $data->theme . '/';
|
|
$this->assetsDir = __DIR__ . '/../../../public/theme/' . $data->theme . '/';
|
|
}
|
|
} |