2020-03-28 20:11:51 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace Venom\Views;
|
|
|
|
|
|
|
|
|
2020-09-10 22:47:58 +02:00
|
|
|
use Venom\Core\ArgumentHandler;
|
|
|
|
use Venom\Core\Config;
|
2020-09-25 21:39:05 +02:00
|
|
|
use Venom\Helper\MetaGenerator;
|
2020-03-28 20:11:51 +01:00
|
|
|
use Venom\Venom;
|
|
|
|
|
|
|
|
class VenomRenderer
|
|
|
|
{
|
|
|
|
private Venom $venom;
|
|
|
|
private ?RenderController $controller;
|
2020-09-25 21:39:05 +02:00
|
|
|
private ?MetaGenerator $metaGenerator;
|
2020-03-28 20:11:51 +01:00
|
|
|
private string $templateData = '';
|
2020-04-03 13:19:26 +02:00
|
|
|
private array $vars = [];
|
2020-03-28 20:11:51 +01:00
|
|
|
private string $baseTemplate = '';
|
|
|
|
private string $templateDir = '';
|
|
|
|
|
|
|
|
public function __construct(Venom $venom)
|
|
|
|
{
|
|
|
|
$this->venom = $venom;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function render(): void
|
|
|
|
{
|
2020-05-31 17:00:05 +02:00
|
|
|
$isAsync = false;
|
2020-03-28 20:11:51 +01:00
|
|
|
if ($this->controller) {
|
|
|
|
ob_start();
|
2020-04-03 13:19:26 +02:00
|
|
|
$this->controller->render($this);
|
|
|
|
$this->templateData = ob_get_clean();
|
2020-05-31 17:00:05 +02:00
|
|
|
$isAsync = $this->controller->getTemplate() === 'async';
|
|
|
|
}
|
|
|
|
if ($isAsync || ArgumentHandler::get()->getItem('async', 'false') === 'true') {
|
|
|
|
echo $this->templateData;
|
|
|
|
exit;
|
2020-03-28 20:11:51 +01:00
|
|
|
}
|
|
|
|
$this->loadBasicTemplate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function loadBasicTemplate(): void
|
|
|
|
{
|
|
|
|
if (file_exists($this->templateDir . $this->baseTemplate)) {
|
|
|
|
include_once $this->templateDir . $this->baseTemplate;
|
|
|
|
} else {
|
2020-04-03 13:19:26 +02:00
|
|
|
echo 'Base Template not found...';
|
2020-03-28 20:11:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 17:15:20 +02:00
|
|
|
public function renderTemplate($template): void
|
|
|
|
{
|
|
|
|
// random variable name... to remove it instantly
|
|
|
|
echo $this->includeTemplate($template, '1408138186');
|
|
|
|
unset($this->vars['1408138186']);
|
|
|
|
}
|
|
|
|
|
2020-04-03 13:19:26 +02:00
|
|
|
/**
|
|
|
|
* 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 '';
|
|
|
|
}
|
|
|
|
|
2020-09-10 22:47:58 +02:00
|
|
|
public function addVar($name, $value): void
|
2020-04-03 13:19:26 +02:00
|
|
|
{
|
|
|
|
$this->vars[$name] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getVar($name)
|
|
|
|
{
|
|
|
|
return $this->vars[$name];
|
|
|
|
}
|
|
|
|
|
2020-09-25 21:33:54 +02:00
|
|
|
public function deleteVar($name): void
|
2020-09-20 17:15:20 +02:00
|
|
|
{
|
|
|
|
unset($this->vars[$name]);
|
|
|
|
}
|
|
|
|
|
2020-03-28 20:11:51 +01:00
|
|
|
public function init(?RenderController $controller): void
|
|
|
|
{
|
|
|
|
$this->controller = $controller;
|
|
|
|
$data = Config::getInstance()->getRenderer();
|
2020-09-25 21:33:54 +02:00
|
|
|
$theme = $data->theme;
|
|
|
|
$base = $data->baseFile ?? 'base';
|
2020-09-25 21:39:05 +02:00
|
|
|
$this->metaGenerator = new MetaGenerator();
|
2020-09-25 21:33:54 +02:00
|
|
|
if (Config::getInstance()->isAdmin()) {
|
|
|
|
$base = 'base';
|
|
|
|
$theme = 'admin';
|
2020-09-25 21:39:05 +02:00
|
|
|
} else {
|
|
|
|
$this->metaGenerator->loadById();
|
2020-09-25 21:33:54 +02:00
|
|
|
}
|
|
|
|
$this->baseTemplate = $base . '.php';
|
|
|
|
$this->templateDir = __DIR__ . '/../../../tpl/' . $theme . '/';
|
2020-03-28 20:11:51 +01:00
|
|
|
}
|
2020-09-20 17:15:20 +02:00
|
|
|
}
|