now right
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/trunk@4 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
93
Sublimer.class.php
Normal file
93
Sublimer.class.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* Простейший шаблонизатор.
|
||||
* Зато быстрый.
|
||||
*
|
||||
*/
|
||||
|
||||
final class Sublimer
|
||||
{
|
||||
const GLOBAL_KEY = 0;
|
||||
|
||||
public $vars = array();
|
||||
public $path = '';
|
||||
public $template = '';
|
||||
|
||||
protected $head_array = array();
|
||||
|
||||
/**
|
||||
* Конструктор
|
||||
*
|
||||
* @param string $path - путь до шаблонов
|
||||
*/
|
||||
public function __construct($path = '')
|
||||
{
|
||||
$this->setPath($path);
|
||||
$this->vars[self::GLOBAL_KEY] = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Присвоить переменную только
|
||||
*
|
||||
* @param string $name - имя переменной
|
||||
* @param mixed $value - значение переменной
|
||||
* @param string $template - шаблон, если не указан, то переменная глобальная
|
||||
*/
|
||||
public function assign($name, $value, $template = self::GLOBAL_KEY)
|
||||
{
|
||||
$this->vars[$template][$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Очистить стек переменных
|
||||
* @param boolean $with_local - включая локальные переменные
|
||||
*/
|
||||
public function clear($template = self::GLOBAL_KEY)
|
||||
{
|
||||
$this->vars[$template] = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Обработать шаблон
|
||||
*
|
||||
* @param string $template - относительный путь до шаблона
|
||||
* @return string - обработанное содержимое шаблона
|
||||
*/
|
||||
public function fetch($template)
|
||||
{
|
||||
$this->template = $template; //дабы экстракт не перезатер нам переменную. Это важно! $this то он не перезатрет :)
|
||||
extract($this->vars[self::GLOBAL_KEY]);
|
||||
if (isset($this->vars[$this->template])) {
|
||||
extract($this->vars[$this->template], EXTR_OVERWRITE);
|
||||
}
|
||||
ob_start();
|
||||
if (!(include $this->path.'/'.$this->template) == 'OK') {
|
||||
throw new MJException('Template '.$this->path.'/'.$this->template.' not found');
|
||||
}
|
||||
return ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Установать путь до шаблонов
|
||||
*
|
||||
* @param string $path - путь до шаблонов
|
||||
*/
|
||||
public function setPath($path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
//Функции для вызова из шаблонов.
|
||||
|
||||
protected function addHead($str)
|
||||
{
|
||||
$this->head_array[] = $str;
|
||||
}
|
||||
|
||||
protected function getHead()
|
||||
{
|
||||
return $this->head_array;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Reference in New Issue
Block a user