<?php class Template { /** * Переменные шаблона * * @var array */ protected $vars; /** * Файл шаблона * * @var string */ private $template; public $tblocks; public function __construct() { $this->vars = array(); $this->template = ''; $this->tblocks = array(); } /** * Объявить переменную шаблона * * @param string $var * @param string $value */ public function set($var, $value = '') { $this->vars[$var] = $value; } /** * Установить файл шаблона * * @param string $tpl */ public function assign($tpl) { $this->template = PATH. 'templates/'. $tpl. '.tpl'; if (file_exists($this->template)) { $this->template = file_get_contents($this->template); $matches = array(); $count = preg_match_all('/{row::([A-Za-z0-9]+)}(.*){\/row::/', $this->template, $matches); if ($count > 0) { for ($i = 0; $i < $count; $i++) { $this->tblocks[$matches[1][$i]] = $matches[2][$i]; } $this->template = preg_replace('/{row::([A-Za-z0-9]+)}(.+){\/row::([A-Za-z0-9]+)}\\n/i', '', $this->template); } } else { echo '<small><b>Error.</b> Template <u>'.basename($this->template). '</u> is not exists!</small><br />'; $this->template = ''; } } /** * Пропарсить шаблон * */ public function display($eval = False) { if ($this->template) { foreach ($this->vars AS $var => $value) { $this->template = str_replace('{'. $var. '}', $value, $this->template); if (empty($value)) { $this->template = preg_replace('/{block::'. $var. '}(.+){\/block::'. $var. '}\\n/i', '', $this->template); } else { $this->template = str_replace(array('{block::'. $var. '}', '{/block::'. $var. '}'), '', $this->template); } } if ($eval) { eval('?>'. $this->template); } else { echo $this->template; } } unset($this->vars); unset($this->tblocks); unset($this->template); } } ?>