From: Mischa POSLAWSKY Date: Thu, 26 Apr 2018 21:05:19 +0000 (+0200) Subject: page: replace placeholders dynamically X-Git-Tag: v3.0~16 X-Git-Url: http://git.shiar.net/minimedit.git/commitdiff_plain/37dbd7898c9c0baf9aec5073b757e3205a6a0c3d page: replace placeholders dynamically Instead of substituting known variables, search contents for placeholders for identical results but allowing for additional options. --- 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