'Home Page', ... ] */ function snippet_include($filename, $context=[]) { $snippet = file_get_contents($filename); // Handle comments {* ... *} $snippet = preg_replace('/ *{\*(.*?)\*}\n*/s','',$snippet); // Use regex to find {placeholders} in the template // Note this uses a callback function to do the matches $snippet = preg_replace_callback('/\{\s*(\w+)\s*\}/', function($matches) use ($context) { $key = $matches[1]; $value = isset($context[$key]) ? $context[$key] : $key; return $value; }, $snippet); return $snippet; }