Add templating system based on native PHP
parent
a80919fdce
commit
cc240f47a2
249
app/HTML.php
249
app/HTML.php
|
@ -1,249 +0,0 @@
|
||||||
<?php
|
|
||||||
class HTML
|
|
||||||
{
|
|
||||||
//Hier wird der HTML Code zum Ausgeben vorbereitet.
|
|
||||||
//Dieser kann aus einer fertigen HTML Seite ausgelesen werden, oder aber auch st<73>ck f<>r St<53>ck
|
|
||||||
//Zusammen gebaut werden.
|
|
||||||
|
|
||||||
//Die Einzelnen Daten k<>nnen nicht direkt von Au<41>en ver<65>ndert werden, sondern m<>ssen durch die Bereitgestellten Optionen gesetzt werden.
|
|
||||||
|
|
||||||
private $HTMLTitle = " "; //Wird in den <header> als <title> Geschrieben.
|
|
||||||
private $StatusMeldung = " "; //Falls Vorhenden eine Statusmeldung vom Script im HTML Text.
|
|
||||||
private $DasMenu = " "; //Beinhaltet das Fertige Men<65>
|
|
||||||
private $DerInhalt = " "; //Beinhaltet den Fertigen Inhalt
|
|
||||||
private $HTMLDatei = " "; //Der inhalt der eingelesen wurde.
|
|
||||||
private $HTMLHeader = " "; //Der HTML HEADER der eingelesen wurde.
|
|
||||||
private $FertigesHTML = " "; //Das Fertige HTML bereit zum Ausgeben.
|
|
||||||
private $isBuild = false; //Hier wird festgehalten ob $FertigesHTML aktuell ist oder nicht.
|
|
||||||
|
|
||||||
//Der <title> wird Generiert.(%%EchoTitle%%)
|
|
||||||
//Dieser wird im HTML Code sp<73>ter als %%HTMLTitle%% aufgerufen.
|
|
||||||
|
|
||||||
public function setHTMLTitle($neuerTitle){
|
|
||||||
//Der Bisherige Title wird komplett <20>berschrieben und gleichzeitig ein neuer Gesetzt.
|
|
||||||
$this->HTMLTitle = $neuerTitle;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addHTMLTitle($Hinzufugen){
|
|
||||||
//Zu dem Bisherigen Titel wird noch etwas am ende hinzugef<65>gt.
|
|
||||||
$this->HTMLTitle = $this->HTMLTitle.$Hinzufugen;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function RemoveHTMLTitle(){
|
|
||||||
//Der Titel wird Komplett gel<65>scht.
|
|
||||||
$this->HTMLTitle = " ";
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//Der HTML HEADER wird Generiert.(%%echoHeader%%)
|
|
||||||
//Dieser wird im HTML Code sp<73>ter als %%echoHeader%% aufgerufen.
|
|
||||||
|
|
||||||
public function setHTMLHeader($neuerHeader){
|
|
||||||
//Der Bisherige Header wird komplett <20>berschrieben und gleichzeitig ein neuer Gesetzt.
|
|
||||||
$this->HTMLHeader = $neuerHeader;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addHTMLHeader($Hinzufugen){
|
|
||||||
//Zu dem Bisherigen Header wird noch etwas am ende hinzugef<65>gt.
|
|
||||||
$this->HTMLHeader = $this->HTMLHeader.$Hinzufugen;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function RemoveHTMLHeader(){
|
|
||||||
//Der Header wird Komplett gel<65>scht.
|
|
||||||
$this->HTMLHeader = " ";
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function importHTMLHeader($file){
|
|
||||||
global $RUNTIME;
|
|
||||||
//Der HTML Header wird aus einer Datei eingelesen und der bisherige gel<65>scht.
|
|
||||||
$this->HTMLHeader = file_get_contents($RUNTIME['BASEDIR'].'/templates/'.$file);
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Der StatusText wird ge<67>ndert.(%%StatusMeldung%%)
|
|
||||||
//Dieser wird im HTML Code sp<73>ter als %%StatusMeldung%% aufgerufen.
|
|
||||||
|
|
||||||
public function setStatusMeldung($neueMeldung){
|
|
||||||
//Die bisherige Status meldung wird komplett <20>berschrieben und gleichzeitig ein neuer Gesetzt.
|
|
||||||
$this->StatusMeldung = $neueMeldung;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function RemoveStatusMeldung(){
|
|
||||||
//Die Meldung wird Komplett gel<65>scht.
|
|
||||||
$this->StatusMeldung = " ";
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Ab hier wird das Men<65> Zusammengebaut. (%%EchoMenu%%)
|
|
||||||
|
|
||||||
public function importTextMenu($neuesMenu){
|
|
||||||
//Das Komplette Men<65> wird direkt importiert und das alte <20>berschreiben.
|
|
||||||
$this->DasMenu = $neuesMenu;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function importHTMLMenu($file){
|
|
||||||
global $RUNTIME;
|
|
||||||
//Das Komplette Men<65> wird aus einer Datei ausgelesen und das alte <20>berschrieben.
|
|
||||||
$this->DasMenu = file_get_contents($RUNTIME['BASEDIR'].'/templates/'.$file);
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addToMenu($html){
|
|
||||||
//Es wird noch etwas ans Men<65> angehengt.
|
|
||||||
$this->DasMenu = $this->DasMenu.$html;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Der Seiten HTML Quelcode wird eingelesen.
|
|
||||||
|
|
||||||
public function importHTML($file){
|
|
||||||
global $RUNTIME;
|
|
||||||
//Der HTML Quelltext wird aus einer Datei eingelesen.
|
|
||||||
$this->HTMLDatei = file_get_contents($RUNTIME['BASEDIR'].'/templates/'.$file);
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setHTML($htmlCode){
|
|
||||||
//Der HTML Quelltext wird direkt gesetzt.
|
|
||||||
$this->HTMLDatei = $htmlCode;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addNachHTML($htmlCode){
|
|
||||||
//Der HTML Quelltext wird direkt gesetzt.
|
|
||||||
$this->HTMLDatei = $this->HTMLDatei.$htmlCode;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addVorHTML($htmlCode){
|
|
||||||
//Der HTML Quelltext wird direkt gesetzt.
|
|
||||||
$this->HTMLDatei = $htmlCode.$this->HTMLDatei;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function DeleteHTML(){
|
|
||||||
//Der HTML Quelltext wird gel<65>scht.
|
|
||||||
$this->HTMLDatei = " ";
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Der inhalt der Seite wird zusammen gesetzt (nicht der quelltext) (%%EchoInhalt%%)
|
|
||||||
|
|
||||||
public function importSeitenInhalt($file){
|
|
||||||
global $RUNTIME;
|
|
||||||
//L<>d einen fertigen Text aus einer datei.
|
|
||||||
$this->DerInhalt = file_get_contents($RUNTIME['BASEDIR'].'/templates/'.$file);
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function setSeitenInhalt($html){
|
|
||||||
//Setz den Seiteninhalt und L<>scht den alten Komplett.
|
|
||||||
$this->DerInhalt = $html;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function importAndAddSeitenInhalt($file){
|
|
||||||
global $RUNTIME;
|
|
||||||
//L<>d einen fertigen Text aus einer datei.
|
|
||||||
$this->DerInhalt = $this->DerInhalt.file_get_contents($RUNTIME['BASEDIR'].'/templates/'.$file);
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function addToSeitenInhalt($html){
|
|
||||||
//Es wird noch weitere Text an den Seiteninhalt angeh<65>ngt.
|
|
||||||
$this->DerInhalt = $this->DerInhalt.$html;
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function GetSeitenInhalt(){
|
|
||||||
//Der Seiteninhalt wird zur<75>ckgegeben.
|
|
||||||
return $this->DerInhalt;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function DeleteSeitenInhalt(){
|
|
||||||
//L<>scht den Seiten inhalt.
|
|
||||||
$this->DerInhalt = " ";
|
|
||||||
$this->isBuild = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function ReplaceSeitenInhalt($tag, $text){
|
|
||||||
//Ersezt Seiten Inhalt
|
|
||||||
$this->DerInhalt = str_replace($tag, $text, $this->DerInhalt);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function ReplaceLayoutInhalt($tag, $text){
|
|
||||||
//Ersezt Layout Inhalt
|
|
||||||
$this->HTMLDatei = str_replace($tag, $text, $this->HTMLDatei);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function CompressHTML(){
|
|
||||||
if($this->isBuild){
|
|
||||||
$this->FertigesHTML = str_replace(" ", "", $this->FertigesHTML);
|
|
||||||
|
|
||||||
$this->FertigesHTML = str_replace(" ", "", $this->FertigesHTML);
|
|
||||||
}else{
|
|
||||||
die("Es kann nur Fertiger HTML Code kompremiert werden.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Hier wird der Fertige HTML Code generiert.
|
|
||||||
//Und alle 3 Teile, Men<65> Titel und inhalt zusammengef<65>gt.
|
|
||||||
public function build(){
|
|
||||||
//Der HTML Code wird zusammen gesetzt.
|
|
||||||
|
|
||||||
$this->FertigesHTML = null; //Der Speicher wird gellert, falls schon einmal Quelltext generiert wurde.
|
|
||||||
$this->FertigesHTML = $this->HTMLDatei; //Und der Unverarbeitete HTML Quelltext eingelesen.
|
|
||||||
|
|
||||||
//Das Men<65> wird in den HTML Quellcode eingef<65>gt.
|
|
||||||
$this->FertigesHTML = str_replace("%%EchoMenu%%", $this->DasMenu, $this->FertigesHTML);
|
|
||||||
|
|
||||||
//Der inhalt wird in den HTML Quellcode eingef<65>gt.
|
|
||||||
$this->FertigesHTML = str_replace("%%EchoInhalt%%", $this->DerInhalt, $this->FertigesHTML);
|
|
||||||
|
|
||||||
//Die Status Meldung wird in den HTML Quellcode eingef<65>gt.
|
|
||||||
$this->FertigesHTML = str_replace("%%StatusMeldung%%", $this->StatusMeldung, $this->FertigesHTML);
|
|
||||||
|
|
||||||
//Der Titel wird in den HTML Quellcode eingef<65>gt.
|
|
||||||
$this->FertigesHTML = str_replace("%%EchoTitle%%", $this->HTMLTitle, $this->FertigesHTML);
|
|
||||||
|
|
||||||
//Der HTML Header wird in den HTML Quellcode eingef<65>gt.
|
|
||||||
$this->FertigesHTML = str_replace("%%echoHeader%%", $this->HTMLHeader, $this->FertigesHTML);
|
|
||||||
|
|
||||||
//Der Titel wird in den HTML Quellcode eingef<65>gt.
|
|
||||||
$this->FertigesHTML = str_replace("%%datum%%", date("Y-m-dTH:i+2"), $this->FertigesHTML);
|
|
||||||
|
|
||||||
//Der Counter wird in den HTML Quellcode eingef<65>gt.
|
|
||||||
$this->FertigesHTML = str_replace("%%GET_SITE%%", isset($_GET['seite']) ? $_GET['seite'] : ' ', $this->FertigesHTML);
|
|
||||||
|
|
||||||
//Die IP Adresse wird in den HTML Quellcode eingef<65>gt.
|
|
||||||
$this->FertigesHTML = str_replace("%%GET_IP%%", isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER['REMOTE_ADDR'] : ' ', $this->FertigesHTML);
|
|
||||||
|
|
||||||
// Add CSRF token
|
|
||||||
$this->FertigesHTML = str_replace("%%CSRF%%", '<input type="hidden" name="csrf" value="'.(isset($_SESSION['csrf']) ? $_SESSION['csrf'] : '').'">', $this->FertigesHTML);
|
|
||||||
|
|
||||||
$this->isBuild = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
//Hier wird der Fertige HTML ausgegeben
|
|
||||||
public function ausgabe(){
|
|
||||||
if($this->isBuild){
|
|
||||||
return $this->FertigesHTML;
|
|
||||||
}else{
|
|
||||||
die("Bitte erst den HTML Code zusammensetzen.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
?>
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Mcp;
|
||||||
|
|
||||||
|
use Mcp\Util\TemplateVarArray;
|
||||||
|
|
||||||
|
class TemplateBuilder
|
||||||
|
{
|
||||||
|
|
||||||
|
private string $basedir;
|
||||||
|
private string $name;
|
||||||
|
private ?string $parent = null;
|
||||||
|
private array $vars = [];
|
||||||
|
|
||||||
|
public function __construct(string $basedir, string $name)
|
||||||
|
{
|
||||||
|
$this->basedir = $basedir;
|
||||||
|
$this->name = $name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function parent(string $parent): TemplateBuilder
|
||||||
|
{
|
||||||
|
$this->parent = $parent;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function vars(array $vars): TemplateBuilder
|
||||||
|
{
|
||||||
|
foreach ($vars as $key => $val) {
|
||||||
|
$this->vars[$key] = htmlspecialchars(strval($val));
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function var(string $key, string $val): TemplateBuilder
|
||||||
|
{
|
||||||
|
$this->vars[$key] = htmlspecialchars($val);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unsafeVar(string $key, string $val): TemplateBuilder
|
||||||
|
{
|
||||||
|
$this->vars[$key] = $val;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render(): void
|
||||||
|
{
|
||||||
|
$v = new TemplateVarArray($this->vars);
|
||||||
|
$basepath = $this->basedir.DIRECTORY_SEPARATOR;
|
||||||
|
if ($this->parent == null) {
|
||||||
|
require $basepath.$this->name;
|
||||||
|
} else {
|
||||||
|
$v['child-template'] = $basepath.$this->name;
|
||||||
|
require $basepath.$this->parent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Mcp\Util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class can be used like a regular array, but is guaranteed to return a value.
|
||||||
|
* Keys not set in the underlying array return an empty string.
|
||||||
|
*/
|
||||||
|
class TemplateVarArray implements \ArrayAccess
|
||||||
|
{
|
||||||
|
|
||||||
|
private array $vars;
|
||||||
|
|
||||||
|
public function __construct(array $vars)
|
||||||
|
{
|
||||||
|
$this->vars = $vars;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetExists(mixed $offset): bool
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetGet(mixed $offset): mixed
|
||||||
|
{
|
||||||
|
return isset($this->vars[$offset]) ? $this->vars[$offset] : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetSet(mixed $offset, mixed $value): void
|
||||||
|
{
|
||||||
|
$this->vars[$offset] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetUnset(mixed $offset): void
|
||||||
|
{
|
||||||
|
unset($this->vars[$offset]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue