projekt templade added

This commit is contained in:
2025-06-12 16:36:11 +02:00
parent d10b697c8c
commit 526b6504a8
22 changed files with 558 additions and 0 deletions

25
Library/ErrorMsg.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
namespace ppa\Library;
use ppa\Library\View;
class ErrorMsg
{
protected $view;
public function __construct($msg = 'Ihre Anfrage konnte nicht verarbeitet werden', $ex = '')
{
$this->view = new \ppa\Library\View(dirname(__DIR__).DIRECTORY_SEPARATOR.'Views'
, 'Error', 'showErrMsg');
$this->view->setVars([
'error' => $msg,
'debug' => $ex
]);
$this->view->render();
}
}

58
Library/View.php Normal file
View File

@@ -0,0 +1,58 @@
<?php
namespace ppa\Library;
class View
{
protected $path, $controller, $do, $vars = [];
/**
* @param string $path Basepath of the views.
* @param string $controllerName Current controller.
* @param string $actionName Current action.
*/
public function __construct($path, $controllerName, $doMethodName)
{
$this->path = $path;
$this->controller = $controllerName;
$this->do = $doMethodName;
}
/**
* Set view vars. The keys will be added, to existing keys.
*
* @param array $vars
*/
public function setVars(array $vars)
{
foreach ($vars as $key => $val) {
$this->vars[$key] = $val;
}
}
public function setDoMethodName($doMethodName)
{
$this->do = $doMethodName;
}
/**
* Render the view.
*
* @throws NotFoundException
*/
public function render()
{
$filename = $this->path.DIRECTORY_SEPARATOR.$this->controller.DIRECTORY_SEPARATOR.$this->do.'.phtml';
if (!file_exists($filename)) {
new \ppa\Library\ErrorMsg("View not found: $filename");
exit();
}
// spare the view the bloat of using "$this->vars[]" for every variable
foreach ($this->vars as $key => $val) {
$$key = $val;
}
include $filename;
}
}