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-11-18 17:50:01 +01:00
|
|
|
use Venom\Helper\TemplateUtil;
|
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 = '';
|
2021-01-03 16:59:11 +01: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
|
2020-11-18 17:50:01 +01:00
|
|
|
echo TemplateUtil::includeTemplate($template);
|
2020-09-20 17:15:20 +02:00
|
|
|
}
|
|
|
|
|
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 = '')
|
|
|
|
{
|
2020-11-18 17:50:01 +01:00
|
|
|
$data = TemplateUtil::includeTemplate($template);
|
|
|
|
$this->vars[$varName] = $data;
|
|
|
|
return $data;
|
2020-04-03 13:19:26 +02:00
|
|
|
}
|
|
|
|
|
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;
|
2020-11-18 17:50:01 +01:00
|
|
|
if (!Config::getInstance()->isAdmin()) {
|
|
|
|
$this->metaGenerator = new MetaGenerator();
|
2020-09-25 21:39:05 +02:00
|
|
|
$this->metaGenerator->loadById();
|
2020-09-25 21:33:54 +02:00
|
|
|
}
|
2020-11-18 17:50:01 +01:00
|
|
|
$util = TemplateUtil::getInstance();
|
|
|
|
$this->templateDir = $util->getDir();
|
|
|
|
$this->baseTemplate = $util->getBase();
|
2020-03-28 20:11:51 +01:00
|
|
|
}
|
2020-09-20 17:15:20 +02:00
|
|
|
}
|