page: common getoutput() to replace placeholders
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 14 Sep 2017 15:44:57 +0000 (17:44 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Mon, 18 Sep 2017 18:21:28 +0000 (20:21 +0200)
page.php

index 4944467684b1673e46060a2dba4094410c6c1634..6d98863165e670b5d55cc8793fb89b89777ad62a 100644 (file)
--- a/page.php
+++ b/page.php
@@ -8,7 +8,7 @@ function fail($error)
        include_once 'page.inc.php';
        ob_start();
        require_once './500.html';
-       print str_replace('[[debug]]', $error, ob_get_clean());
+       print getoutput(['debug' => $error]);
 }
 set_exception_handler('fail');
 register_shutdown_function(function () {
@@ -58,6 +58,15 @@ print "</div>\n\n";
 # execute dynamic code
 
 if ($Page) {
+       function getoutput($blocks = [])
+       {
+               $rep = [];
+               foreach ($blocks as $name => $html) {
+                       $rep["[[$name]]"] = $html;
+               }
+               return str_replace(array_keys($rep), array_values($rep), ob_get_clean());
+       }
+
        $found |= require "./$Page.php";
 }
 
@@ -74,8 +83,7 @@ if (!$found) {
                http_response_code(404);
                ob_start();
                require "./404.html";
-               $url = htmlspecialchars($_SERVER['REQUEST_URI']);
-               print str_replace('[[url]]', $url, ob_get_clean());
+               print getoutput([ 'url' => htmlspecialchars($_SERVER['REQUEST_URI']) ]);
        }
 }