function getoutput($blocks = [])
{
- $rep = [];
- foreach ($blocks as $name => $html) {
- $rep["[[$name]]"] = sprintf('<!--BLOCK:%s-->%s<!--/-->',
- is_numeric($name) ? '' : "[[$name]]",
- preg_replace('{<!--[^-]*-->}', '', $html)
- );
- }
$doc = ob_get_clean();
+
if (!empty($blocks['warn'])) {
$warn = '<p class="warn">[[warn]]</p>';
if ($offset = strpos($doc, '</h2>')) {
$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('<!--BLOCK:%s-->%s<!--/-->',
+ is_numeric($name) ? '' : $placeholder, # edit replacement
+ preg_replace('{<!--[^-]*-->}', '', $html) # contents
+ );
+ },
+ $doc
+ );
}
# custom error handling