summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/engelsystem_provider.php12
-rw-r--r--includes/pages/guest_credits.php2
-rw-r--r--includes/pages/user_shifts.php2
-rw-r--r--includes/sys_template.php22
-rw-r--r--public/index.php2
-rw-r--r--src/Renderer/EngineInterface.php21
-rw-r--r--src/Renderer/HtmlEngine.php34
-rw-r--r--src/Renderer/Renderer.php62
-rw-r--r--src/helpers.php17
-rw-r--r--templates/maintenance.html (renamed from public/maintenance.html)0
10 files changed, 148 insertions, 26 deletions
diff --git a/includes/engelsystem_provider.php b/includes/engelsystem_provider.php
index edfae705..ff682871 100644
--- a/includes/engelsystem_provider.php
+++ b/includes/engelsystem_provider.php
@@ -4,6 +4,8 @@ use Engelsystem\Config\Config;
use Engelsystem\Database\Db;
use Engelsystem\Exceptions\Handler as ExceptionHandler;
use Engelsystem\Http\Request;
+use Engelsystem\Renderer\HtmlEngine;
+use Engelsystem\Renderer\Renderer;
/**
* This file includes all needed functions, connects to the db etc.
@@ -43,12 +45,20 @@ $request::setInstance($request);
* Check for maintenance
*/
if ($config->get('maintenance')) {
- echo file_get_contents(__DIR__ . '/../public/maintenance.html');
+ echo file_get_contents(__DIR__ . '/../templates/maintenance.html');
die();
}
/**
+ * Initialize renderer
+ */
+$renderer = new Renderer();
+$renderer->addRenderer(new HtmlEngine());
+Renderer::setInstance($renderer);
+
+
+/**
* Register error handler
*/
$errorHandler = new ExceptionHandler();
diff --git a/includes/pages/guest_credits.php b/includes/pages/guest_credits.php
index d9224cbb..db86132d 100644
--- a/includes/pages/guest_credits.php
+++ b/includes/pages/guest_credits.php
@@ -13,5 +13,5 @@ function credits_title()
*/
function guest_credits()
{
- return template_render(__DIR__ . '/../../templates/guest_credits.html', []);
+ return view(__DIR__ . '/../../templates/guest_credits.html');
}
diff --git a/includes/pages/user_shifts.php b/includes/pages/user_shifts.php
index 4dabdfb5..813cb9b3 100644
--- a/includes/pages/user_shifts.php
+++ b/includes/pages/user_shifts.php
@@ -206,7 +206,7 @@ function view_user_shifts()
return page([
div('col-md-12', [
msg(),
- template_render(__DIR__ . '/../../templates/user_shifts.html', [
+ view(__DIR__ . '/../../templates/user_shifts.html', [
'title' => shifts_title(),
'room_select' => make_select($rooms, $shiftsFilter->getRooms(), 'rooms', _('Rooms')),
'start_select' => html_select_key('start_day', 'start_day', array_combine($days, $days), $start_day),
diff --git a/includes/sys_template.php b/includes/sys_template.php
index bcf462d7..a659a7f3 100644
--- a/includes/sys_template.php
+++ b/includes/sys_template.php
@@ -322,28 +322,6 @@ function table_buttons($buttons = [])
}
/**
- * Load and render template
- *
- * @param string $file
- * @param string[] $data
- * @return string
- */
-function template_render($file, $data = [])
-{
- if (file_exists($file)) {
- $template = file_get_contents($file);
- if (is_array($data)) {
- foreach ($data as $name => $content) {
- $template = str_replace('%' . $name . '%', $content, $template);
- }
- }
- return $template;
- }
- engelsystem_error('Cannot find template file «' . $file . '».');
- return '';
-}
-
-/**
* @param string $str
* @param int $length
* @return string
diff --git a/public/index.php b/public/index.php
index 52f32438..f22721c8 100644
--- a/public/index.php
+++ b/public/index.php
@@ -166,7 +166,7 @@ if (
$event_config = EventConfig();
-echo template_render(__DIR__ . '/../templates/layout.html', [
+echo view(__DIR__ . '/../templates/layout.html', [
'theme' => isset($user) ? $user['color'] : config('theme'),
'title' => $title,
'atom_link' => ($page == 'news' || $page == 'user_meetings')
diff --git a/src/Renderer/EngineInterface.php b/src/Renderer/EngineInterface.php
new file mode 100644
index 00000000..ca468db5
--- /dev/null
+++ b/src/Renderer/EngineInterface.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace Engelsystem\Renderer;
+
+interface EngineInterface
+{
+ /**
+ * Render a template
+ *
+ * @param string $path
+ * @param mixed[] $data
+ * @return string
+ */
+ public function get($path, $data = []);
+
+ /**
+ * @param string $path
+ * @return bool
+ */
+ public function canRender($path);
+}
diff --git a/src/Renderer/HtmlEngine.php b/src/Renderer/HtmlEngine.php
new file mode 100644
index 00000000..4a48e1f0
--- /dev/null
+++ b/src/Renderer/HtmlEngine.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace Engelsystem\Renderer;
+
+class HtmlEngine implements EngineInterface
+{
+ /**
+ * Render a template
+ *
+ * @param string $path
+ * @param mixed[] $data
+ * @return string
+ */
+ public function get($path, $data = [])
+ {
+ $template = file_get_contents($path);
+ if (is_array($data)) {
+ foreach ($data as $name => $content) {
+ $template = str_replace('%' . $name . '%', $content, $template);
+ }
+ }
+
+ return $template;
+ }
+
+ /**
+ * @param string $path
+ * @return bool
+ */
+ public function canRender($path)
+ {
+ return strpos($path, '.html') && file_exists($path);
+ }
+}
diff --git a/src/Renderer/Renderer.php b/src/Renderer/Renderer.php
new file mode 100644
index 00000000..bf3d5609
--- /dev/null
+++ b/src/Renderer/Renderer.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace Engelsystem\Renderer;
+
+use ErrorException;
+
+class Renderer
+{
+ /** @var self */
+ protected static $instance;
+
+ /** @var EngineInterface[] */
+ protected $renderer = [];
+
+ /**
+ * Render a template
+ *
+ * @param string $template
+ * @param mixed[] $data
+ * @return string
+ */
+ public function render($template, $data = [])
+ {
+ foreach ($this->renderer as $renderer) {
+ if (!$renderer->canRender($template)) {
+ continue;
+ }
+
+ return $renderer->get($template, $data);
+ }
+
+ engelsystem_error('Unable to find a renderer for template file &laquo;' . $template . '&raquo;.');
+ return '';
+ }
+
+ /**
+ * Add a new renderer engine
+ *
+ * @param EngineInterface $renderer
+ */
+ public function addRenderer(EngineInterface $renderer)
+ {
+ $this->renderer[] = $renderer;
+ }
+
+ /**
+ * @return self
+ * @throws ErrorException
+ */
+ public static function getInstance()
+ {
+ return self::$instance;
+ }
+
+ /**
+ * @param self $instance
+ */
+ public static function setInstance($instance)
+ {
+ self::$instance = $instance;
+ }
+}
diff --git a/src/helpers.php b/src/helpers.php
index a410b27e..d44d1d21 100644
--- a/src/helpers.php
+++ b/src/helpers.php
@@ -3,6 +3,7 @@
use Engelsystem\Config\Config;
use Engelsystem\Http\Request;
+use Engelsystem\Renderer\Renderer;
/**
* Get or set config values
@@ -39,3 +40,19 @@ function request($key = null, $default = null)
return $request->input($key, $default);
}
+
+/**
+ * @param string $template
+ * @param mixed[] $data
+ * @return Renderer|string
+ */
+function view($template = null, $data = null)
+{
+ $renderer = Renderer::getInstance();
+
+ if (is_null($template)) {
+ return $renderer;
+ }
+
+ return $renderer->render($template, $data);
+}
diff --git a/public/maintenance.html b/templates/maintenance.html
index 3f7dae8a..3f7dae8a 100644
--- a/public/maintenance.html
+++ b/templates/maintenance.html