cg view
This commit is contained in:
56
kernel/CgView.php
Normal file
56
kernel/CgView.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace kernel;
|
||||
|
||||
class CgView
|
||||
{
|
||||
public string $viewPath = '';
|
||||
public bool|string $layout = false;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function render(string $view, array $data = []): void
|
||||
{
|
||||
$content = $this->createContent($view, $data);
|
||||
|
||||
echo $content;
|
||||
}
|
||||
|
||||
public function fetch(string $view, array $data = []): false|string
|
||||
{
|
||||
$content = $this->createContent($view, $data);
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
private function createContent(string $view, array $data = []): false|string
|
||||
{
|
||||
ob_start();
|
||||
|
||||
foreach ($data as $key => $datum){
|
||||
${"$key"} = $datum;
|
||||
}
|
||||
|
||||
include ($this->viewPath . $view);
|
||||
|
||||
$content = ob_get_contents();
|
||||
ob_end_clean ();
|
||||
|
||||
ob_start();
|
||||
$file_content = $content;
|
||||
|
||||
if ($this->layout){
|
||||
if (file_exists($this->viewPath . $this->layout)){
|
||||
include ($this->viewPath . $this->layout);
|
||||
$file_content = ob_get_contents();
|
||||
}
|
||||
}
|
||||
ob_end_clean ();
|
||||
|
||||
return $file_content;
|
||||
}
|
||||
|
||||
}
|
@ -2,19 +2,30 @@
|
||||
|
||||
namespace kernel;
|
||||
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
|
||||
class Controller
|
||||
{
|
||||
protected \Twig\Loader\FilesystemLoader $loader;
|
||||
protected \Twig\Environment $twig;
|
||||
protected CgView $cgView;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->loader = new \Twig\Loader\FilesystemLoader(ROOT_DIR . $_ENV['VIEWS_PATH']);
|
||||
$this->twig = new \Twig\Environment($this->loader, ['cache' => ROOT_DIR . $_ENV['VIEWS_CACHE_PATH']]);
|
||||
|
||||
$this->cgView = new CgView();
|
||||
$this->cgView->viewPath = ROOT_DIR . "/views";
|
||||
|
||||
$this->init();
|
||||
}
|
||||
|
||||
public function redirect(string $url): void
|
||||
#[NoReturn] protected function redirect(string $url, int $code = 301): void
|
||||
{
|
||||
header("Location: " . $url);
|
||||
header('Location: ' . $url, true, $code);
|
||||
exit;
|
||||
}
|
||||
|
||||
protected function init(){}
|
||||
}
|
24
kernel/IGTabel/btn/PrimaryBtn.php
Normal file
24
kernel/IGTabel/btn/PrimaryBtn.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace kernel\IGTabel\btn;
|
||||
|
||||
class PrimaryBtn
|
||||
{
|
||||
protected string $btn = '';
|
||||
|
||||
public function __construct(string $title, string $url)
|
||||
{
|
||||
$this->btn = "<a class='btn btn-primary' href='$url' style='margin: 3px; width: 100px;' >$title</a>";
|
||||
}
|
||||
|
||||
public function fetch(): string
|
||||
{
|
||||
return $this->btn;
|
||||
}
|
||||
|
||||
public static function create(string $title, string $url): PrimaryBtn
|
||||
{
|
||||
return new self($title, $url);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user