From 37dbd7898c9c0baf9aec5073b757e3205a6a0c3d Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Thu, 26 Apr 2018 23:05:19 +0200 Subject: [PATCH] page: replace placeholders dynamically Instead of substituting known variables, search contents for placeholders for identical results but allowing for additional options. --- page.php | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/page.php b/page.php index 76a99b5..c6eee87 100644 --- a/page.php +++ b/page.php @@ -10,14 +10,8 @@ function abort($body, $status = NULL) { function getoutput($blocks = []) { - $rep = []; - foreach ($blocks as $name => $html) { - $rep["[[$name]]"] = sprintf('%s', - is_numeric($name) ? '' : "[[$name]]", - preg_replace('{}', '', $html) - ); - } $doc = ob_get_clean(); + if (!empty($blocks['warn'])) { $warn = '

[[warn]]

'; if ($offset = strpos($doc, '')) { @@ -27,7 +21,19 @@ function getoutput($blocks = []) $doc = $warn . "\n\n" . $doc; } } - return str_replace(array_keys($rep), array_values($rep), $doc); + + return preg_replace_callback( + '< \[\[ ([^]]*) \]\] >x', + function ($sub) use ($blocks) { + list ($placeholder, $name) = $sub; + $html = $blocks[$name]; + return sprintf('%s', + is_numeric($name) ? '' : $placeholder, # edit replacement + preg_replace('{}', '', $html) # contents + ); + }, + $doc + ); } # custom error handling -- 2.30.0