. class Templater { public $mode; public $setup; public $template; protected $curlyRegex = '/\{([a-z]+):([a-z_-]+)\}/i'; protected $newlineSearch = array ("\r\n", "\r", "\n"); protected $newlineReplace = array ("\n", "\n", PHP_EOL); public function __construct ($mode = 'php', Setup $setup) { $this->mode = $mode; $this->setup = $setup; } public function loadFile ($file) { // Attempt to read template HTML file $theme_html = @file_get_contents ($file); // Check if template file read successfully if ($theme_html !== false) { // If so, return trimmed HTML template return trim ($theme_html); } else { // If not, throw exception throw new \Exception ('Failed to load template file.'); } } protected function curlyVariable ($key) { return '{{' . $key . '}}'; } protected function fromTemplate ($key) { if ($this->mode !== 'php') { return $this->curlyVariable ($key); } if (!empty ($this->template[$key])) { return $this->template[$key]; } return ''; } protected function placeholder ($id) { $span_id = 'hashover-placeholder-' . $id; $span_id .= '-' . $this->fromTemplate ('permalink'); $placeholder = new HTMLTag ('span', array ( 'id' => $span_id ), false); if (!empty ($this->template[$id])) { $placeholder->innerHTML ($this->template[$id]); } return $placeholder->asHTML (); } protected function parser ($var) { if (empty ($var[1]) or empty ($var[2])) { return ''; } switch ($var[1]) { case 'hashover': { return $this->fromTemplate ($var[2]); } case 'placeholder': { return $this->placeholder ($var[2]); } } } public function parseTemplate ($file, array $template = array ()) { $this->template = $template; $data = $this->loadFile ($file); $template = preg_replace_callback ($this->curlyRegex, 'self::parser', $data); $template = str_replace ($this->newlineSearch, $this->newlineReplace, $template); $template = preg_replace ('/^[\s\n]+$/m', '', $template); return $template; } 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 HTML as template $theme = $this->parseTemplate ($path, $template); return $theme; } }