- added error_reporting if devMode

- added TestController.php
- added temp function to load controllers (should load later via seo urls)
- added baseTemplate for default look and feel
- removed adding file extension -> always need to be .php!
This commit is contained in:
VersusTune 2020-04-03 13:19:26 +02:00
parent 9be884c8e4
commit 7ba4e3e0a6
8 changed files with 97 additions and 14 deletions

View file

@ -40,7 +40,7 @@ $config->setMail([
// all themes are in __DIR__/public/theme/
$config->setRender([
'theme' => 'default', //very important! it will search for a folder with this name.
'baseFile' => 'base.php', //this will called after all templates are rendered...
'baseFile' => 'base', //this will called after all templates are rendered...
'useCache' => false, //is only on big systems good
'cacheName' => 'defaultCache', //this is for bigger systems, ignore it
]);

View file

@ -4,4 +4,6 @@
$modules = [];
// register controllers that can handle templates ;) need to have a render function for this
$controllers = [];
$controllers = [
'test' => \Modules\TestController::class
];

View file

@ -22,6 +22,11 @@ if ($config->isMaintenance()) {
echo 'Currently not available';
exit;
}
//if devMode is on show all errors!
if ($config->isDevMode()) {
error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);
}
$venom = new Venom();
$venom->initModules($modules);
$venom->initControllers($controllers);

View file

@ -22,10 +22,18 @@ class Venom
{
// we need to load the current controller and the current start template.
// after this we can start the renderer
$this->renderer->init(null);
$this->renderer->init($this->findController());
$this->renderer->render();
}
private function findController(): ?RenderController
{
if (isset($_GET['cl'], $this->controllers[$_GET['cl']])) {
return $this->controllers[$_GET['cl']];
}
return null;
}
public function initModules(array $modules): void
{
foreach ($modules as $key => $moduleClass) {
@ -50,9 +58,4 @@ class Venom
{
}
private function findController()
{
}
}

View file

@ -8,7 +8,7 @@ interface RenderController
{
public function register(): bool;
public function render(): string;
public function render(VenomRenderer $renderer): bool;
public function getTemplate(): string;
}

View file

@ -12,6 +12,7 @@ class VenomRenderer
private Venom $venom;
private ?RenderController $controller;
private string $templateData = '';
private array $vars = [];
private string $baseTemplate = '';
private string $templateDir = '';
private string $assetsDir = '';
@ -25,8 +26,8 @@ class VenomRenderer
{
if ($this->controller) {
ob_start();
$this->controller->render();
$this->templateData = ob_end_clean();
$this->controller->render($this);
$this->templateData = ob_get_clean();
}
$this->loadBasicTemplate();
}
@ -36,16 +37,45 @@ class VenomRenderer
if (file_exists($this->templateDir . $this->baseTemplate)) {
include_once $this->templateDir . $this->baseTemplate;
} else {
echo "Base Template not found...";
echo $this->templateData;
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)
{
$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'] ?? 'base.php';
$this->baseTemplate = $data['baseFile'] . '.php' ?? 'base.php';
$this->templateDir = __DIR__ . '/../../../tpl/' . $data['theme'] . '/';
$this->assetsDir = __DIR__ . '/../../../public/theme/' . $data['theme'] . '/';
}

View file

@ -0,0 +1,27 @@
<?php
namespace Modules;
use Venom\Views\RenderController;
use Venom\Views\VenomRenderer;
class TestController implements RenderController
{
public function register(): bool
{
return true;
}
public function render(VenomRenderer $renderer): bool
{
return true;
}
public function getTemplate(): string
{
return 'base.php';
}
}

16
tpl/default/base.php Normal file
View file

@ -0,0 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>VenomBase</title>
</head>
<body>
<header>
VenomCMS
</header>
<?= $this->templateData ?>
</body>
</html>