. class Templater { // Setup information protected $setup; // Store arguments passed during instantiation public function __construct (Setup $setup) { $this->setup = $setup; } // Reads a file public function loadFile ($file) { // Attempt to read template HTML file $content = @file_get_contents ($file); // Check if template file read successfully if ($content !== false) { // If so, return trimmed HTML template return trim ($content); } else { // If not, throw exception throw new \Exception ( 'Failed to load template file.' ); } } // Parses file from any location public function parseTemplate ($file, array $template = array ()) { // Read template file $data = $this->loadFile ($file); // Local callback for preg_replace $parser = function ($grp) use (&$template) { // Store key for pretty code $key = $grp[2]; // Store whitespace for pretty code $whitespace = $grp[1]; // Return data from template if it exists if (!empty ($template[$key])) { return $whitespace . $template[$key]; } // Otherwise, return nothing return ''; }; // Curly brace variable regular expression $curly_regex = '/(\s*)\{([a-z_-]+)\}/i'; // Convert string to OS-specific line endings $template = preg_replace ('/\r\n|\r|\n/', PHP_EOL, $template); // Replace curly brace variable with data from template $template = preg_replace_callback ($curly_regex, $parser, $data); // Remove blank lines $template = preg_replace ('/^[\s\n]+$/m', '', $template); return $template; } // Parses file from the theme directory public function parseTheme ($file, array $template = array ()) { // Get the file path for the configured theme $path = $this->setup->getThemePath ($file, false); $path = $this->setup->getAbsolutePath ($path); // Parse the theme file as template if (!empty ($template)) { return $this->parseTemplate ($path, $template); } // Otherwise, return theme file as-is return $this->loadFile ($path); } }